N = int(eval(input()))

src = [tuple(map(int,input().split())) for i in range(N)]



xs = []

ys = []

for i,(x,y) in enumerate(src):

    xs.append((x,i))

    ys.append((y,i))

xs.sort()

ys.sort()



es = []

for (x1,i1),(x2,i2) in zip(xs,xs[1:]):

    es.append((x2-x1, i1, i2))

for (y1,i1),(y2,i2) in zip(ys,ys[1:]):

    es.append((y2-y1, i1, i2))

es.sort()



class UnionFind:

    def __init__(self,N):

        self.parent = [i for i in range(N)]

        self.rank = [0] * N

        self.count = 0

    def root(self,a):

        if self.parent[a] == a:

            return a

        else:

            self.parent[a] = self.root(self.parent[a])

            return self.parent[a]

    def is_same(self,a,b):

        return self.root(a) == self.root(b)

    def unite(self,a,b):

        ra = self.root(a)

        rb = self.root(b)

        if ra == rb: return

        if self.rank[ra] < self.rank[rb]:

            self.parent[ra] = rb

        else:

            self.parent[rb] = ra

            if self.rank[ra] == self.rank[rb]: self.rank[ra] += 1

        self.count += 1



ans = 0

uf = UnionFind(N)

for d,a,b in es:

    if uf.is_same(a,b): continue

    uf.unite(a,b)

    ans += d

print(ans)
import sys

input = sys.stdin.readline

N = int(eval(input()))

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



class UnionFind:

    def __init__(self,N):

        self.parent = [i for i in range(N)]

        self._size = [1] * N

        self.count = 0

    def root(self,a):

        if self.parent[a] == a:

            return a

        else:

            self.parent[a] = self.root(self.parent[a])

            return self.parent[a]

    def is_same(self,a,b):

        return self.root(a) == self.root(b)

    def unite(self,a,b):

        ra = self.root(a)

        rb = self.root(b)

        if ra == rb: return

        if self._size[ra] < self._size[rb]: ra,rb = rb,ra

        self._size[ra] += self._size[rb]

        self.parent[rb] = ra

        self.count += 1

    def size(self,a):

        return self._size[self.root(a)]



uf = UnionFind(N)

sx = sorted(XY, key=lambda x:x[0])

sy = sorted(XY, key=lambda x:x[1])



es = []

for (x1,_,i),(x2,_,j) in zip(sx,sx[1:]):

    es.append((x2-x1, i, j))

for (_,y1,i),(_,y2,j) in zip(sy,sy[1:]):

    es.append((y2-y1, i, j))

es.sort(key=lambda x:x[0])



ans = 0

for d,i,j in es:

    if uf.is_same(i,j): continue

    uf.unite(i,j)

    ans += d

print(ans)
N = int(eval(input()))
src = [tuple(map(int, input().split())) for i in range(N)]
xs = []
ys = []
for i, (x, y) in enumerate(src):
    xs.append((x, i))
    ys.append((y, i))
xs.sort()
ys.sort()
es = []
for (x1, i1), (x2, i2) in zip(xs, xs[1:]):
    es.append((x2 - x1, i1, i2))
for (y1, i1), (y2, i2) in zip(ys, ys[1:]):
    es.append((y2 - y1, i1, i2))
es.sort()


class UnionFind:
    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self.rank = [0] * N
        self.count = 0

    def root(self, a):
        if self.parent[a] == a:
            return a
        else:
            self.parent[a] = self.root(self.parent[a])
            return self.parent[a]

    def is_same(self, a, b):
        return self.root(a) == self.root(b)

    def unite(self, a, b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb:
            return
        if self.rank[ra] < self.rank[rb]:
            self.parent[ra] = rb
        else:
            self.parent[rb] = ra
            if self.rank[ra] == self.rank[rb]:
                self.rank[ra] += 1
        self.count += 1


ans = 0
uf = UnionFind(N)
for d, a, b in es:
    if uf.is_same(a, b):
        continue
    uf.unite(a, b)
    ans += d
print(ans)

import sys

input = sys.stdin.readline
N = int(eval(input()))
XY = [tuple(map(int, input().split() + [i])) for i in range(N)]


class UnionFind:
    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self._size = [1] * N
        self.count = 0

    def root(self, a):
        if self.parent[a] == a:
            return a
        else:
            self.parent[a] = self.root(self.parent[a])
            return self.parent[a]

    def is_same(self, a, b):
        return self.root(a) == self.root(b)

    def unite(self, a, b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb:
            return
        if self._size[ra] < self._size[rb]:
            ra, rb = rb, ra
        self._size[ra] += self._size[rb]
        self.parent[rb] = ra
        self.count += 1

    def size(self, a):
        return self._size[self.root(a)]


uf = UnionFind(N)
sx = sorted(XY, key=lambda x: x[0])
sy = sorted(XY, key=lambda x: x[1])
es = []
for (x1, _, i), (x2, _, j) in zip(sx, sx[1:]):
    es.append((x2 - x1, i, j))
for (_, y1, i), (_, y2, j) in zip(sy, sy[1:]):
    es.append((y2 - y1, i, j))
es.sort(key=lambda x: x[0])
ans = 0
for d, i, j in es:
    if uf.is_same(i, j):
        continue
    uf.unite(i, j)
    ans += d
print(ans)

+import sys + +input = sys.stdin.readline -src = [tuple(map(int, input().split())) for i in range(N)] -xs = [] -ys = [] -for i, (x, y) in enumerate(src): -    xs.append((x, i)) -    ys.append((y, i)) -xs.sort() -ys.sort() -es = [] -for (x1, i1), (x2, i2) in zip(xs, xs[1:]): -    es.append((x2 - x1, i1, i2)) -for (y1, i1), (y2, i2) in zip(ys, ys[1:]): -    es.append((y2 - y1, i1, i2)) -es.sort() +XY = [tuple(map(int, input().split() + [i])) for i in range(N)] -        self.rank = [0] * N +        self._size = [1] * N -        if self.rank[ra] < self.rank[rb]: -            self.parent[ra] = rb -        else: -            self.parent[rb] = ra -            if self.rank[ra] == self.rank[rb]: -                self.rank[ra] += 1 +        if self._size[ra] < self._size[rb]: +            ra, rb = rb, ra +        self._size[ra] += self._size[rb] +        self.parent[rb] = ra +    def size(self, a): +        return self._size[self.root(a)] + +uf = UnionFind(N) +sx = sorted(XY, key=lambda x: x[0]) +sy = sorted(XY, key=lambda x: x[1]) +es = [] +for (x1, _, i), (x2, _, j) in zip(sx, sx[1:]): +    es.append((x2 - x1, i, j)) +for (_, y1, i), (_, y2, j) in zip(sy, sy[1:]): +    es.append((y2 - y1, i, j)) +es.sort(key=lambda x: x[0]) -uf = UnionFind(N) -for d, a, b in es: -    if uf.is_same(a, b): +for d, i, j in es: +    if uf.is_same(i, j): -    uf.unite(a, b) +    uf.unite(i, j)