### BUGGY CODE ###

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
#N=int(input())
#X,Y=map(int,input().split()))
#S=list(map(int,input().split()))
#S=[list(map(int,input().split())) for i in range(N)]

N=int(input())
XY=[list(map(int,input().split()))+[i] for i in range(N)]
Xsort=sorted([[i[0],i[2]] for i in XY],key=lambda x:x[0])
Ysort=sorted([[i[1],i[2]] for i in XY],key=lambda x:x[0])
Nidxlst=[[0,0] for i in range(N)]
for i in range(N):
    idx=Xsort[i][1]
    Nidxlst[idx][0]=i
    idx=Ysort[i][1]
    Nidxlst[idx][1]=i



value=0
x,y=Nidxlst[0]
ittenai=set([i for i in range(1,N)])

stack=[]
heapify(stack)
setI=set()
dic={}

def main(x,y):
    if x>0:
        m=Xsort[x][0]-Xsort[x-1][0]

        a=Xsort[x-1][1]
        if a in ittenai:
            heappush(stack,m)
            if m in dic :

                dic[m].append(a)
            else:
                dic.update({m:[a]})
    if x<N-1:
        m=Xsort[x+1][0]-Xsort[x][0]
        a=Xsort[x+1][1]
        if a in ittenai:
            heappush(stack,m)
            if m in dic :

                dic[m].append(a)
            else:
                dic.update({m:[a]})
    if y>0:
        m=Ysort[y][0]-Ysort[y-1][0]
        a=Ysort[y-1][1]
        if a in ittenai:
            heappush(stack,m)
            if m in dic :

                dic[m].append(a)
            else:
                dic.update({m:[a]})
    if y<N-1:
        m=Ysort[y+1][0]-Ysort[y][0]
        a=Ysort[y+1][1]
        if a in ittenai:
            heappush(stack,m)
            if m in dic :

                dic[m].append(a)
            else:
                dic.update({m:[a]})
main(x,y)
idle=10**9+1
value=0
while ittenai:
    m=heappop(stack)
    if dic[m]:
        a=dic[m].pop()
        if a in ittenai:
            x,y=Nidxlst[a]
            main(x,y)
            ittenai.remove(a)
            value+=m

print(value)


### FIXED CODE ###

#from collections import *
#from itertools import *
#from bisect import *
from heapq import *
#import copy
#N=int(input())
#X,Y=map(int,input().split()))
#S=list(map(int,input().split()))
#S=[list(map(int,input().split())) for i in range(N)]

N=int(input())

Xsort=[]
Ysort=[]
for i in range(N):
    X,Y=map(int,input().split())
    Xsort.append([X,i])
    Ysort.append([Y,i])
Xsort=sorted(Xsort,key=lambda x:x[0])
Ysort=sorted(Ysort,key=lambda x:x[0])
Nidxlst=[[0,0] for i in range(N)]
for i in range(N):
    idx=Xsort[i][1]
    Nidxlst[idx][0]=i
    idx=Ysort[i][1]
    Nidxlst[idx][1]=i



value=0
x,y=Nidxlst[0]
ittenai=set([i for i in range(1,N)])

stack=[]
heapify(stack)
setm=set()
dic={}
def main(x,y):
    if x>0:
        a=Xsort[x-1][1]
        if a in ittenai:
            m=Xsort[x][0]-Xsort[x-1][0]
            heappush(stack,m)
            if m in setm :
                dic[m].append(a)
            else:
                setm.add(m)
                dic.update({m:[a]})
    if x<N-1:
        a=Xsort[x+1][1]
        if a in ittenai:
            m=Xsort[x+1][0]-Xsort[x][0]
            heappush(stack,m)
            if m in setm :
                dic[m].append(a)
            else:
                setm.add(m)
                dic.update({m:[a]})
    if y>0:
        a=Ysort[y-1][1]
        if a in ittenai:
            m=Ysort[y][0]-Ysort[y-1][0]
            heappush(stack,m)
            if m in setm :
                dic[m].append(a)
            else:
                setm.add(m)
                dic.update({m:[a]})
    if y<N-1:
        a=Ysort[y+1][1]
        if a in ittenai:
            m=Ysort[y+1][0]-Ysort[y][0]
            heappush(stack,m)
            if m in setm :
                dic[m].append(a)
            else:
                setm.add(m)
                dic.update({m:[a]})
main(x,y)
idle=10**9+1
value=0
while ittenai:
    m=heappop(stack)
    a=dic[m].pop()
    if a in ittenai:
        x,y=Nidxlst[a]
        main(x,y)
        ittenai.remove(a)
        value+=m

print(value)