
n = int(input())
a = []*n

for i in range(n):
    x,y = map(int,input().split())
    a.append([x,y,i])

e = [0]*n
a.sort()

for i in range(n-1):
    idx1 = a[i][2]
    idx2 = a[i+1][2]
    c = abs(a[i][0]-a[i+1][0])
    e[idx1] = [idx1,idx2,c]
a.sort(key=lambda a:a[1])

for i in range(n-1):
    idx1 = a[i][2]
    idx2 = a[i+1][2]
    c = abs(a[i][1]-a[i+1][1])
    if e[idx1] :
        if e[idx1][2] > c:
            e[idx1] = [idx1,idx2,c]
    else:
        e[idx1] = [idx1,idx2,c]

e.sort(key=lambda e:e[2])
l = [0]*n
ans = 0
for i in range(len(e)):
    idx1 = e[i][0]
    idx2 = e[i][1]
    c = e[i][2]
    if l[idx1] == 0 or l[idx2] == 0:
        ans += c
        l[idx1] = 1
        l[idx2] = 1

print(ans)