class unionfind:

    def __init__(self, n):

        self.n = n

        self.root = [-1]*(n+1)

        self.rnk = [0]*(n+1)

    def find(self, x):

        if(self.root[x] < 0):

            return x

        else:

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

            return self.root[x]

    def unite(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if(x == y):

            return 

        elif(self.rnk[x] > self.rnk[y]):

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

            self.root[y] = x

        else:

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

            self.root[x] = y

            if(self.rnk[x] == self.rnk[y]):

                self.rnk[y] += 1

    def same(self, x, y):

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

    def count(self, x):

        return -self.root[self.find(x)]

import sys

input=sys.stdin.readline

n=int(eval(input()))

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

l.sort()

import collections

d=collections.defaultdict(int)

j=1

for i in l:

    if d[i]==0:

        d[i]=j

        j+=1

pq=[]

for i in range(n-1):

    pq.append((l[i+1][0]-l[i][0],d[l[i]],d[l[i+1]]))

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

for i in range(n-1):

    pq.append((l[i+1][1]-l[i][1],d[l[i]],d[l[i+1]]))

ans=0

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

uf=unionfind(n)

for c,x,y in pq:

    if not uf.same(x,y):

        ans+=c

        uf.unite(x,y)

print(ans)
class unionfind:

    def __init__(self, n):

        self.n = n

        self.root = [-1]*(n+1)

        self.rnk = [0]*(n+1)

    def find(self, x):

        if(self.root[x] < 0):

            return x

        else:

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

            return self.root[x]

    def unite(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if(x == y):

            return 

        elif(self.rnk[x] > self.rnk[y]):

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

            self.root[y] = x

        else:

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

            self.root[x] = y

            if(self.rnk[x] == self.rnk[y]):

                self.rnk[y] += 1

    def same(self, x, y):

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

    def count(self, x):

        return -self.root[self.find(x)]

import sys

input=sys.stdin.readline

n=int(eval(input()))

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

l.sort()

import collections

d=collections.defaultdict(int)

j=1

for i in l:

    if d[i]==0:

        d[i]=j

        j+=1

pq=[]

for i in range(n-1):

    pq.append((l[i+1][0]-l[i][0],d[l[i]],d[l[i+1]]))

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

for i in range(n-1):

    pq.append((l[i+1][1]-l[i][1],d[l[i]],d[l[i+1]]))

ans=0

pq.sort()

uf=unionfind(n)

for c,x,y in pq:

    if not uf.same(x,y):

        ans+=c

        uf.unite(x,y)

print(ans)
class unionfind:
    def __init__(self, n):
        self.n = n
        self.root = [-1] * (n + 1)
        self.rnk = [0] * (n + 1)

    def find(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1

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

    def count(self, x):
        return -self.root[self.find(x)]


import sys

input = sys.stdin.readline
n = int(eval(input()))
l = [tuple(map(int, input().split())) for i in range(n)]
l.sort()
import collections

d = collections.defaultdict(int)
j = 1
for i in l:
    if d[i] == 0:
        d[i] = j
        j += 1
pq = []
for i in range(n - 1):
    pq.append((l[i + 1][0] - l[i][0], d[l[i]], d[l[i + 1]]))
l.sort(key=lambda x: x[1])
for i in range(n - 1):
    pq.append((l[i + 1][1] - l[i][1], d[l[i]], d[l[i + 1]]))
ans = 0
pq.sort(key=lambda x: x[0])
uf = unionfind(n)
for c, x, y in pq:
    if not uf.same(x, y):
        ans += c
        uf.unite(x, y)
print(ans)

class unionfind:
    def __init__(self, n):
        self.n = n
        self.root = [-1] * (n + 1)
        self.rnk = [0] * (n + 1)

    def find(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1

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

    def count(self, x):
        return -self.root[self.find(x)]


import sys

input = sys.stdin.readline
n = int(eval(input()))
l = [tuple(map(int, input().split())) for i in range(n)]
l.sort()
import collections

d = collections.defaultdict(int)
j = 1
for i in l:
    if d[i] == 0:
        d[i] = j
        j += 1
pq = []
for i in range(n - 1):
    pq.append((l[i + 1][0] - l[i][0], d[l[i]], d[l[i + 1]]))
l.sort(key=lambda x: x[1])
for i in range(n - 1):
    pq.append((l[i + 1][1] - l[i][1], d[l[i]], d[l[i + 1]]))
ans = 0
pq.sort()
uf = unionfind(n)
for c, x, y in pq:
    if not uf.same(x, y):
        ans += c
        uf.unite(x, y)
print(ans)

-pq.sort(key=lambda x: x[0]) +pq.sort()