import sys





class UnionFind():

    def __init__(self, n):

        self.parents = [-1] * n



    def find(self, x):

        if self.parents[x] < 0:

            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:

            return



        if self.parents[x] > self.parents[y]:

            x, y = y, x



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

        self.parents[y] = x





N = int(eval(input()))

Px = []

Py = []

for k, s in enumerate(sys.stdin.readlines()):

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

    Px.append((x, k))

    Py.append((y, k))



edge = []



Px.sort()

Py.sort()

for i in range(N - 1):

    x0, k0 = Px[i]

    x1, k1 = Px[i + 1]

    d = abs(x0 - x1)

    edge.append((d, k0, k1))



    y0, k0 = Py[i]

    y1, k1 = Py[i + 1]

    d = abs(y0 - y1)

    edge.append((d, k0, k1))



edge.sort()

uf = UnionFind(N)

cnt = 0

for d, k0, k1 in edge:

    if uf.find(k0) != uf.find(k1):

        uf.union(k0, k1)

        cnt += d

    

    if uf.parents[uf.find(0)] == -N:

        break



print(cnt)

import sys





class UnionFind():

    def __init__(self, n):

        self.parents = [-1] * n



    def find(self, x):

        if self.parents[x] < 0:

            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:

            return



        if self.parents[x] > self.parents[y]:

            x, y = y, x



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

        self.parents[y] = x





N = int(eval(input()))

Px = []

Py = []

for k, s in enumerate(sys.stdin.readlines()):

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

    Px.append((x, k))

    Py.append((y, k))



edge = []



Px.sort()

Py.sort()

for i in range(N - 1):

    x0, k0 = Px[i]

    x1, k1 = Px[i + 1]

    d = abs(x0 - x1)

    edge.append((d, k0, k1))



    y0, k0 = Py[i]

    y1, k1 = Py[i + 1]

    d = abs(y0 - y1)

    edge.append((d, k0, k1))



edge.sort()

uf = UnionFind(N)

cnt = 0

for d, k0, k1 in edge:

    if uf.find(k0) != uf.find(k1):

        uf.union(k0, k1)

        cnt += d



print(cnt)

import sys


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

    def find(self, x):
        if self.parents[x] < 0:
            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:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x


N = int(eval(input()))
Px = []
Py = []
for k, s in enumerate(sys.stdin.readlines()):
    x, y = list(map(int, s.split()))
    Px.append((x, k))
    Py.append((y, k))
edge = []
Px.sort()
Py.sort()
for i in range(N - 1):
    x0, k0 = Px[i]
    x1, k1 = Px[i + 1]
    d = abs(x0 - x1)
    edge.append((d, k0, k1))
    y0, k0 = Py[i]
    y1, k1 = Py[i + 1]
    d = abs(y0 - y1)
    edge.append((d, k0, k1))
edge.sort()
uf = UnionFind(N)
cnt = 0
for d, k0, k1 in edge:
    if uf.find(k0) != uf.find(k1):
        uf.union(k0, k1)
        cnt += d
    if uf.parents[uf.find(0)] == -N:
        break
print(cnt)

import sys


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

    def find(self, x):
        if self.parents[x] < 0:
            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:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x


N = int(eval(input()))
Px = []
Py = []
for k, s in enumerate(sys.stdin.readlines()):
    x, y = list(map(int, s.split()))
    Px.append((x, k))
    Py.append((y, k))
edge = []
Px.sort()
Py.sort()
for i in range(N - 1):
    x0, k0 = Px[i]
    x1, k1 = Px[i + 1]
    d = abs(x0 - x1)
    edge.append((d, k0, k1))
    y0, k0 = Py[i]
    y1, k1 = Py[i + 1]
    d = abs(y0 - y1)
    edge.append((d, k0, k1))
edge.sort()
uf = UnionFind(N)
cnt = 0
for d, k0, k1 in edge:
    if uf.find(k0) != uf.find(k1):
        uf.union(k0, k1)
        cnt += d
print(cnt)

-    if uf.parents[uf.find(0)] == -N: -        break