class UnionFind():

    def __init__(self,n):

        self.n=n

        self.par=[i for i in range(n)]

        self.size=[1]*n

    def find(self,x):

        if x==self.par[x]:

            return self.par[x]

        else:

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

            return self.par[x]

    def unite(self,x,y):

        x=self.find(x)

        y=self.find(y)

        if x==y:

            return

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

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

            self.par[y]=x

        else:

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

            self.par[x]=y



import sys

input=sys.stdin.buffer.readline

n=int(eval(input()))



x_list=[]

y_list=[]



for i in range(n):

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

    x_list.append((x,i))

    y_list.append((y,i))

x_list.sort()

y_list.sort()

edges=[]



for i in range(1,n):

    edges.append((x_list[i][0]-x_list[i-1][0],x_list[i][1],x_list[i-1][1]))



    edges.append((y_list[i][0]-y_list[i-1][0],y_list[i][1],y_list[i-1][1]))



edges.sort()

uf=UnionFind(n)

ans=0

for e in edges:

    wt=e[0]

    x=e[1]

    y=e[2]

    if uf.find(x)==uf.find(y):

        continue

    ans+=wt

    uf.unite(x,y)

print(ans)



class UnionFind():

    def __init__(self,n):

        self.n=n

        self.par=[i for i in range(n)]

        self.size=[1]*n

    def find(self,x):

        if x==self.par[x]:

            return self.par[x]

        else:

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

            return self.par[x]

    def unite(self,x,y):

        x=self.find(x)

        y=self.find(y)

        if x==y:

            return

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

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

            self.par[y]=x

        else:

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

            self.par[x]=y



n=int(eval(input()))



x_list=[]

y_list=[]



for i in range(n):

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

    x_list.append((x,i))

    y_list.append((y,i))

x_list.sort()

y_list.sort()

edges=[]



for i in range(1,n):

    edges.append((x_list[i][0]-x_list[i-1][0],x_list[i][1],x_list[i-1][1]))



    edges.append((y_list[i][0]-y_list[i-1][0],y_list[i][1],y_list[i-1][1]))



edges.sort()

uf=UnionFind(n)

ans=0

for e in edges:

    wt=e[0]

    x=e[1]

    y=e[2]

    if uf.find(x)==uf.find(y):

        continue

    ans+=wt

    uf.unite(x,y)

print(ans)



class UnionFind:
    def __init__(self, n):
        self.n = n
        self.par = [i for i in range(n)]
        self.size = [1] * n

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.size[x] > self.size[y]:
            self.size[x] += self.size[y]
            self.par[y] = x
        else:
            self.size[y] += self.size[x]
            self.par[x] = y


import sys

input = sys.stdin.buffer.readline
n = int(eval(input()))
x_list = []
y_list = []
for i in range(n):
    x, y = list(map(int, input().split()))
    x_list.append((x, i))
    y_list.append((y, i))
x_list.sort()
y_list.sort()
edges = []
for i in range(1, n):
    edges.append((x_list[i][0] - x_list[i - 1][0], x_list[i][1], x_list[i - 1][1]))
    edges.append((y_list[i][0] - y_list[i - 1][0], y_list[i][1], y_list[i - 1][1]))
edges.sort()
uf = UnionFind(n)
ans = 0
for e in edges:
    wt = e[0]
    x = e[1]
    y = e[2]
    if uf.find(x) == uf.find(y):
        continue
    ans += wt
    uf.unite(x, y)
print(ans)

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.par = [i for i in range(n)]
        self.size = [1] * n

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.size[x] > self.size[y]:
            self.size[x] += self.size[y]
            self.par[y] = x
        else:
            self.size[y] += self.size[x]
            self.par[x] = y


n = int(eval(input()))
x_list = []
y_list = []
for i in range(n):
    x, y = list(map(int, input().split()))
    x_list.append((x, i))
    y_list.append((y, i))
x_list.sort()
y_list.sort()
edges = []
for i in range(1, n):
    edges.append((x_list[i][0] - x_list[i - 1][0], x_list[i][1], x_list[i - 1][1]))
    edges.append((y_list[i][0] - y_list[i - 1][0], y_list[i][1], y_list[i - 1][1]))
edges.sort()
uf = UnionFind(n)
ans = 0
for e in edges:
    wt = e[0]
    x = e[1]
    y = e[2]
    if uf.find(x) == uf.find(y):
        continue
    ans += wt
    uf.unite(x, y)
print(ans)

-import sys - -input = sys.stdin.buffer.readline