class UnionFind():

    def __init__(self,n):

        self.n=n

        self.parents = [i for i in range(n+1)]

        self.size = [1]*(n+1)

    def find(self,x):

        if self.parents[x]==x:

            return x

        else:

            self.parents[x]=self.find(self.parents[x])

            return self.parents[x]

    def unite(self,x,y):

        xRoot = self.find(x)

        yRoot = self.find(y)

        if xRoot == yRoot:

            return

        if self.size[xRoot]>self.size[yRoot]:

            self.parents[yRoot] = xRoot

        else:

            self.parents[xRoot] = yRoot

            if self.size[xRoot] == self.size[yRoot]:

                self.size[yRoot]+=1



N = int(eval(input()))

edges = []

nodes = []

for i in range(N):

    x,y = list(map(int,input().split()))

    nodes.append((i,x,y))

nodes.sort(key=lambda x: x[1])

for i in range(1,N):

    w = nodes[i][1]-nodes[i-1][1]

    edges.append((w,nodes[i][0],nodes[i-1][0]))

nodes.sort(key=lambda x: x[2])

for i in range(1,N):

    w = nodes[i][2] - nodes[i-1][2]

    edges.append((w,nodes[i][0],nodes[i-1][0]))

edges.sort()

tree = UnionFind(N)

cost = 0

for w,a,b in edges:

    if tree.find(a) != tree.find(b):

        tree.unite(a,b)

        cost += w

print(cost)
class UnionFind():

    def __init__(self,n):

        self.n=n

        self.parents = [i for i in range(n+1)]

        self.size = [1]*(n+1)

    def find(self,x):

        if self.parents[x]==x:

            return x

        else:

            self.parents[x]=self.find(self.parents[x])

            return self.parents[x]

    def unite(self,x,y):

        xRoot = self.find(x)

        yRoot = self.find(y)

        if xRoot == yRoot:

            return

        if self.size[xRoot]>self.size[yRoot]:

            self.parents[yRoot] = xRoot

            self.size[xRoot] += self.size[yRoot]

            self.size[yRoot] = 0

        else:

            self.parents[xRoot] = yRoot

            self.size[yRoot] += self.size[xRoot]

            self.size[xRoot] = 0



N = int(eval(input()))

XY = [[i]+list(map(int,input().split())) for i in range(N)]

edges = []

XY.sort(key=lambda x: x[1])

for j in range(1,N):

    edges.append((XY[j-1][0],XY[j][0],XY[j][1]-XY[j-1][1]))

XY.sort(key=lambda x: x[2])

for j in range(1,N):

    edges.append((XY[j-1][0],XY[j][0],XY[j][2]-XY[j-1][2]))



cost = 0

edges.sort(key=lambda x: x[2])

tree = UnionFind(N)

for i,j,c in edges:

    if tree.find(i)!=tree.find(j):

        tree.unite(i,j)

        cost += c



print(cost)
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [i for i in range(n + 1)]
        self.size = [1] * (n + 1)

    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def unite(self, x, y):
        xRoot = self.find(x)
        yRoot = self.find(y)
        if xRoot == yRoot:
            return
        if self.size[xRoot] > self.size[yRoot]:
            self.parents[yRoot] = xRoot
        else:
            self.parents[xRoot] = yRoot
            if self.size[xRoot] == self.size[yRoot]:
                self.size[yRoot] += 1


N = int(eval(input()))
edges = []
nodes = []
for i in range(N):
    x, y = list(map(int, input().split()))
    nodes.append((i, x, y))
nodes.sort(key=lambda x: x[1])
for i in range(1, N):
    w = nodes[i][1] - nodes[i - 1][1]
    edges.append((w, nodes[i][0], nodes[i - 1][0]))
nodes.sort(key=lambda x: x[2])
for i in range(1, N):
    w = nodes[i][2] - nodes[i - 1][2]
    edges.append((w, nodes[i][0], nodes[i - 1][0]))
edges.sort()
tree = UnionFind(N)
cost = 0
for w, a, b in edges:
    if tree.find(a) != tree.find(b):
        tree.unite(a, b)
        cost += w
print(cost)

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [i for i in range(n + 1)]
        self.size = [1] * (n + 1)

    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def unite(self, x, y):
        xRoot = self.find(x)
        yRoot = self.find(y)
        if xRoot == yRoot:
            return
        if self.size[xRoot] > self.size[yRoot]:
            self.parents[yRoot] = xRoot
            self.size[xRoot] += self.size[yRoot]
            self.size[yRoot] = 0
        else:
            self.parents[xRoot] = yRoot
            self.size[yRoot] += self.size[xRoot]
            self.size[xRoot] = 0


N = int(eval(input()))
XY = [[i] + list(map(int, input().split())) for i in range(N)]
edges = []
XY.sort(key=lambda x: x[1])
for j in range(1, N):
    edges.append((XY[j - 1][0], XY[j][0], XY[j][1] - XY[j - 1][1]))
XY.sort(key=lambda x: x[2])
for j in range(1, N):
    edges.append((XY[j - 1][0], XY[j][0], XY[j][2] - XY[j - 1][2]))
cost = 0
edges.sort(key=lambda x: x[2])
tree = UnionFind(N)
for i, j, c in edges:
    if tree.find(i) != tree.find(j):
        tree.unite(i, j)
        cost += c
print(cost)

+            self.size[xRoot] += self.size[yRoot] +            self.size[yRoot] = 0 -            if self.size[xRoot] == self.size[yRoot]: -                self.size[yRoot] += 1 +            self.size[yRoot] += self.size[xRoot] +            self.size[xRoot] = 0 +XY = [[i] + list(map(int, input().split())) for i in range(N)] -nodes = [] -for i in range(N): -    x, y = list(map(int, input().split())) -    nodes.append((i, x, y)) -nodes.sort(key=lambda x: x[1]) -for i in range(1, N): -    w = nodes[i][1] - nodes[i - 1][1] -    edges.append((w, nodes[i][0], nodes[i - 1][0])) -nodes.sort(key=lambda x: x[2]) -for i in range(1, N): -    w = nodes[i][2] - nodes[i - 1][2] -    edges.append((w, nodes[i][0], nodes[i - 1][0])) -edges.sort() +XY.sort(key=lambda x: x[1]) +for j in range(1, N): +    edges.append((XY[j - 1][0], XY[j][0], XY[j][1] - XY[j - 1][1])) +XY.sort(key=lambda x: x[2]) +for j in range(1, N): +    edges.append((XY[j - 1][0], XY[j][0], XY[j][2] - XY[j - 1][2])) +cost = 0 +edges.sort(key=lambda x: x[2]) -cost = 0 -for w, a, b in edges: -    if tree.find(a) != tree.find(b): -        tree.unite(a, b) -        cost += w +for i, j, c in edges: +    if tree.find(i) != tree.find(j): +        tree.unite(i, j) +        cost += c