from collections import defaultdict
n = input()
cs = []
for i in xrange(n):
    xi, yi = map(int, raw_input().split())
    cs.append((i, xi, yi))


csx = sorted(cs, key=lambda c: c[1])
csy = sorted(cs, key=lambda c: c[2])


xd = []
yd = []
for i in xrange(n-1):
    xd.append((csx[i+1][1] - csx[i][1], (csx[i], csx[i+1])))
    yd.append((csy[i+1][2] - csy[i][2], (csy[i], csy[i+1])))


diff = sorted(xd + yd)


res = 0
memo = defaultdict(int)
i = 0
c = 0

while c != n-1:
    if memo[diff[i][1][0][0]] >= 1 and memo[diff[i][1][1][0]] >= 1:
        i += 1
        continue
    memo[diff[i][1][0][0]] += 1
    memo[diff[i][1][1][0]] += 1
    res += diff[i][0]
    i += 1
    c += 1

print res

