import copy
if __name__ == '__main__':
    n = int(input())
    p = [None for i in range(n)]
    for i in range(n):
        p[i] = list(map(int,input().split()))
        p[i].append(i)
    p = sorted(p,key=lambda x:x[0])
    con = p[0]
    coni = [0]
    notused = list(range(1,n))
    d = [[min(abs(p[i][0]-p[0][0]),abs(p[i][1]-p[0][1])),i] for i in range(n)]
    cost = 0
    while len(notused)>0:
        #print(coni)
        #print(notused)
        sortd = sorted(d,key=lambda x:x[0])
        #print(sortd)
        for i in range(n):
            if sortd[i][1] in notused:
                nearind = sortd[i][1]
                break
        minc = 1000000000
        for i in coni:
            if min(abs(p[nearind][0]-p[i][0]),abs(p[nearind][1]-p[i][1]))<minc:
                minc = min(abs(p[nearind][0]-p[i][0]),abs(p[nearind][1]-p[i][1]))
        cost += minc
        con.append(p[nearind])
        coni.append(nearind)
        notused.remove(nearind)
        d = [[min(d[i][0],min(abs(p[nearind][0]-p[i][0]),abs(p[nearind][1]-p[i][1]))),i] for i in range(n)]
        #print(cost)
    print(cost)
