from heapq import heappush, heappop





class UnionFind():

    def __init__(self, n):

        self.parents = list(range(n))



    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 union(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if x != y:

            self.parents[y] = x



N = int(eval(input()))

q = []

ans = 0

S = UnionFind(N)

x = []

y = []



for i in range(N):

    p,r = list(map(int, input().split()))

    x.append([p,i])

    y.append([r,i])



x.sort()

y.sort()

for i in range(N-1):

    x1, i1 = x[i]

    y1, j1 = y[i]

    x2, i2 = x[i+1]

    y2, j2 = y[i+1]

    heappush(q, (abs(x1-x2), i1, i2))

    heappush(q, (abs(y1-y2), j1, j2))



heappush(q, (abs(x[N-1][0]-x[0][0]), x[N-1][1], x[0][1]))

heappush(q, (abs(y[N-1][0]-y[0][0]), y[N-1][1], y[0][1]))

# クラスカル法

while len(q) > 0:

    d, a, b = heappop(q)

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

        ans += d

        S.union(a, b)

print(ans)
from heapq import heappop, heappush



class UnionFind():

    def __init__(self, n):

        self.parents = list(range(n))

        self.size = [1] * n



    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 union(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if x != y:

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

                x,y = y,x

            self.parents[y] = x

            self.size[x] += self.size[y]



N = int(eval(input()))

x = [None] * N

y = [None] * N

for i in range(N):

    a, b = list(map(int, input().split()))

    x[i] = (a,i)

    y[i] = (b,i)



x.sort()

y.sort()

q = []

for i in range(N-1):

    heappush(q, (abs(x[i][0]-x[i+1][0]), x[i][1], x[i+1][1]))

    heappush(q, (abs(y[i][0]-y[i+1][0]), y[i][1], y[i+1][1]))



uf = UnionFind(N)

ans = 0

# クラスカル法

while len(q) > 0:

    w, s, t = heappop(q)

    if uf.find(s) != uf.find(t):

        ans += w

        uf.union(s,t)

print(ans)
from heapq import heappush, heappop


class UnionFind:
    def __init__(self, n):
        self.parents = list(range(n))

    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 union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            self.parents[y] = x


N = int(eval(input()))
q = []
ans = 0
S = UnionFind(N)
x = []
y = []
for i in range(N):
    p, r = list(map(int, input().split()))
    x.append([p, i])
    y.append([r, i])
x.sort()
y.sort()
for i in range(N - 1):
    x1, i1 = x[i]
    y1, j1 = y[i]
    x2, i2 = x[i + 1]
    y2, j2 = y[i + 1]
    heappush(q, (abs(x1 - x2), i1, i2))
    heappush(q, (abs(y1 - y2), j1, j2))
heappush(q, (abs(x[N - 1][0] - x[0][0]), x[N - 1][1], x[0][1]))
heappush(q, (abs(y[N - 1][0] - y[0][0]), y[N - 1][1], y[0][1]))
# クラスカル法
while len(q) > 0:
    d, a, b = heappop(q)
    if S.find(a) != S.find(b):
        ans += d
        S.union(a, b)
print(ans)

from heapq import heappop, heappush


class UnionFind:
    def __init__(self, n):
        self.parents = list(range(n))
        self.size = [1] * n

    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 union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.size[x] < self.size[y]:
                x, y = y, x
            self.parents[y] = x
            self.size[x] += self.size[y]


N = int(eval(input()))
x = [None] * N
y = [None] * N
for i in range(N):
    a, b = list(map(int, input().split()))
    x[i] = (a, i)
    y[i] = (b, i)
x.sort()
y.sort()
q = []
for i in range(N - 1):
    heappush(q, (abs(x[i][0] - x[i + 1][0]), x[i][1], x[i + 1][1]))
    heappush(q, (abs(y[i][0] - y[i + 1][0]), y[i][1], y[i + 1][1]))
uf = UnionFind(N)
ans = 0
# クラスカル法
while len(q) > 0:
    w, s, t = heappop(q)
    if uf.find(s) != uf.find(t):
        ans += w
        uf.union(s, t)
print(ans)

-from heapq import heappush, heappop +from heapq import heappop, heappush +        self.size = [1] * n +            if self.size[x] < self.size[y]: +                x, y = y, x +            self.size[x] += self.size[y] -q = [] -ans = 0 -S = UnionFind(N) -x = [] -y = [] +x = [None] * N +y = [None] * N -    p, r = list(map(int, input().split())) -    x.append([p, i]) -    y.append([r, i]) +    a, b = list(map(int, input().split())) +    x[i] = (a, i) +    y[i] = (b, i) +q = [] -    x1, i1 = x[i] -    y1, j1 = y[i] -    x2, i2 = x[i + 1] -    y2, j2 = y[i + 1] -    heappush(q, (abs(x1 - x2), i1, i2)) -    heappush(q, (abs(y1 - y2), j1, j2)) -heappush(q, (abs(x[N - 1][0] - x[0][0]), x[N - 1][1], x[0][1])) -heappush(q, (abs(y[N - 1][0] - y[0][0]), y[N - 1][1], y[0][1])) +    heappush(q, (abs(x[i][0] - x[i + 1][0]), x[i][1], x[i + 1][1])) +    heappush(q, (abs(y[i][0] - y[i + 1][0]), y[i][1], y[i + 1][1])) +uf = UnionFind(N) +ans = 0 -    d, a, b = heappop(q) -    if S.find(a) != S.find(b): -        ans += d -        S.union(a, b) +    w, s, t = heappop(q) +    if uf.find(s) != uf.find(t): +        ans += w +        uf.union(s, t)