import heapq as hq

class UnionFind(object):

    def __init__(self, size):

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

        self.rank = [0]*size

    def find(self, x):

        if self.parent[x] == x:

            return x

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

        return self.parent[x]

    def unite(self, x, y):

        x, y = self.find(x), self.find(y)

        if x == y:

            return

        if self.rank[x] < self.rank[y]:

            self.parent[x] = y

        else:

            self.parent[y] = x

            if self.rank[x] == self.rank[y]:

                self.rank[x] += 1

    def same(self, x, y):

        return self.find(x) == self.find(y)

N = int(eval(input()))

ls = []

rs = []

ind = 0

inds = dict()

for _ in range(N):

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

    if (x,y) not in list(inds.keys()):

        ls.append((x,y))

        rs.append((y,x))

        inds[(x,y)] = ind

        ind += 1

N = ind

ls.sort()

rs.sort()

uf = UnionFind(N)



edges = []

for i in range(N-1):

    x = ls[i][0]

    a = ls[i+1][0]

    y = ls[i][1]

    b = ls[i+1][1]

    d = min(abs(x-a), abs(y-b))

    edges.append((d,(x,y),(a,b)))

    x = rs[i][1]

    a = rs[i+1][1]

    y = rs[i][0]

    b = rs[i+1][0]

    d = min(abs(x-a), abs(y-b))

    edges.append((d,(x,y),(a,b)))

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

ans = 0

for d,i,j in edges:

        # 閉路ができない場合追加する

        if not uf.same(inds[i],inds[j]):

            uf.unite(inds[i],inds[j])

            ans += d

print(ans)
import sys

input = sys.stdin.readline

class UnionFind(object):

    def __init__(self, size):

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

        self.rank = [0]*size

    def find(self, x):

        if self.parent[x] == x:

            return x

        # path compression

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

        return self.parent[x]

    

    def unite(self, x, y):

        x, y = self.find(x), self.find(y)

        if x == y:

            return

        # union by rank

        if self.rank[x] < self.rank[y]:

            self.parent[x] = y

        else:

            self.parent[y] = x

            if self.rank[x] == self.rank[y]:

                self.rank[x] += 1

    

    def same(self, x, y):

        return self.find(x) == self.find(y)



if __name__ == "__main__":

    N = int(eval(input()))

    vs = []

    for i in range(N):

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

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

    vs.sort()

    edges = []

    for k in range(1, N):

        x,y,i = vs[k]

        a,b,j = vs[k-1]

        edges.append((i, j, min(abs(x-a),abs(y-b))))

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

    for k in range(1, N):

        x,y,i = vs[k]

        a,b,j = vs[k-1]

        edges.append((i, j, min(abs(x-a),abs(y-b))))

    uf = UnionFind(N)

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

    ans = 0

    for i,j,w in edges:

        if not uf.same(i,j):

            uf.unite(i,j)

            ans += w

    print(ans)
import heapq as hq


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

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

    def unite(self, x, y):
        x, y = self.find(x), self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def same(self, x, y):
        return self.find(x) == self.find(y)


N = int(eval(input()))
ls = []
rs = []
ind = 0
inds = dict()
for _ in range(N):
    x, y = list(map(int, input().split()))
    if (x, y) not in list(inds.keys()):
        ls.append((x, y))
        rs.append((y, x))
        inds[(x, y)] = ind
        ind += 1
N = ind
ls.sort()
rs.sort()
uf = UnionFind(N)
edges = []
for i in range(N - 1):
    x = ls[i][0]
    a = ls[i + 1][0]
    y = ls[i][1]
    b = ls[i + 1][1]
    d = min(abs(x - a), abs(y - b))
    edges.append((d, (x, y), (a, b)))
    x = rs[i][1]
    a = rs[i + 1][1]
    y = rs[i][0]
    b = rs[i + 1][0]
    d = min(abs(x - a), abs(y - b))
    edges.append((d, (x, y), (a, b)))
edges.sort(key=lambda x: x[0])
ans = 0
for d, i, j in edges:
    # 閉路ができない場合追加する
    if not uf.same(inds[i], inds[j]):
        uf.unite(inds[i], inds[j])
        ans += d
print(ans)

import sys

input = sys.stdin.readline


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

    def find(self, x):
        if self.parent[x] == x:
            return x
        # path compression
        self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def unite(self, x, y):
        x, y = self.find(x), self.find(y)
        if x == y:
            return
        # union by rank
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def same(self, x, y):
        return self.find(x) == self.find(y)


if __name__ == "__main__":
    N = int(eval(input()))
    vs = []
    for i in range(N):
        x, y = list(map(int, input().split()))
        vs.append((x, y, i))
    vs.sort()
    edges = []
    for k in range(1, N):
        x, y, i = vs[k]
        a, b, j = vs[k - 1]
        edges.append((i, j, min(abs(x - a), abs(y - b))))
    vs.sort(key=lambda x: x[1])
    for k in range(1, N):
        x, y, i = vs[k]
        a, b, j = vs[k - 1]
        edges.append((i, j, min(abs(x - a), abs(y - b))))
    uf = UnionFind(N)
    edges.sort(key=lambda x: x[2])
    ans = 0
    for i, j, w in edges:
        if not uf.same(i, j):
            uf.unite(i, j)
            ans += w
    print(ans)

-import heapq as hq +import sys + +input = sys.stdin.readline +        # path compression +        # union by rank -N = int(eval(input())) -ls = [] -rs = [] -ind = 0 -inds = dict() -for _ in range(N): -    x, y = list(map(int, input().split())) -    if (x, y) not in list(inds.keys()): -        ls.append((x, y)) -        rs.append((y, x)) -        inds[(x, y)] = ind -        ind += 1 -N = ind -ls.sort() -rs.sort() -uf = UnionFind(N) -edges = [] -for i in range(N - 1): -    x = ls[i][0] -    a = ls[i + 1][0] -    y = ls[i][1] -    b = ls[i + 1][1] -    d = min(abs(x - a), abs(y - b)) -    edges.append((d, (x, y), (a, b))) -    x = rs[i][1] -    a = rs[i + 1][1] -    y = rs[i][0] -    b = rs[i + 1][0] -    d = min(abs(x - a), abs(y - b)) -    edges.append((d, (x, y), (a, b))) -edges.sort(key=lambda x: x[0]) -ans = 0 -for d, i, j in edges: -    # 閉路ができない場合追加する -    if not uf.same(inds[i], inds[j]): -        uf.unite(inds[i], inds[j]) -        ans += d -print(ans) +if __name__ == "__main__": +    N = int(eval(input())) +    vs = [] +    for i in range(N): +        x, y = list(map(int, input().split())) +        vs.append((x, y, i)) +    vs.sort() +    edges = [] +    for k in range(1, N): +        x, y, i = vs[k] +        a, b, j = vs[k - 1] +        edges.append((i, j, min(abs(x - a), abs(y - b)))) +    vs.sort(key=lambda x: x[1]) +    for k in range(1, N): +        x, y, i = vs[k] +        a, b, j = vs[k - 1] +        edges.append((i, j, min(abs(x - a), abs(y - b)))) +    uf = UnionFind(N) +    edges.sort(key=lambda x: x[2]) +    ans = 0 +    for i, j, w in edges: +        if not uf.same(i, j): +            uf.unite(i, j) +            ans += w +    print(ans)