import numpy
N=int(input())
len = [list(map(int,input().split())) for t in range (N)]
a=[]
ans=0
for i in range (N):
    for j in range (N):
        if i <= j:
            a.append(abs(len[i][1]-len[j][1]))
        else:
            a.append(abs(len[i][0]-len[j][0]))

b=[i for i in range(N)]
a=[max(a)+1 if i==0 else i for i in a]

while all((x == b[0]  for x in b))==False:
    c=min(a)
    for i in range (N*N):
        if b[i//N] != b[i%N]:
            if a[i]==min(a):
                ans+=a[i]
                a[i//N+i%N*N]=max(a) #反対側のつなぎ方を0に
                a[i]=max(a)
                b=[b[i//N] if x==b[i%N] else x for x in b]
    else:
        if c == min(a):
            a=[max(a) if y==min(a) else y for y in a]

print(ans)
