N = int(input())
x,y = [],[]
INF = 100000000000
for i in range(N):
    s,t = [int(k) for k in input().split(" ")]
    x.append(s)
    y.append(t)
dic_x = {}
dic_y = {}
for i in range(N):
    for j in range(i+1,N):
        dic_x[(i,j)] = abs(x[i]-x[j])
        dic_y[(i,j)] = abs(y[i]-y[j])
edge_x=[]
for key,value in sorted(dic_x.items(),key=lambda s:s[1]):
    edge_x.append(key)
edge_y=[]
for key,value in sorted(dic_y.items(),key=lambda s:s[1]):
    edge_y.append(key)
itr_x = 0
itr_y = 0
V = []
ans=0
while len(V) != N :
    if dic_x[edge_x[itr_x]] < dic_y[edge_y[itr_y]]:
        i = edge_x[itr_x][0]
        j = edge_x[itr_x][1]
        itr_x += 1
        if (i in V) == False and (j in V) == False:
            V.append(i)
            V.append(j)
            ans += dic_x[(i,j)]
        elif (i in V) == False:
            V.append(i)
            ans += dic_x[(i,j)]
        elif (j in V) == False:
            V.append(j)
            ans += dic_x[(i,j)]
    else:
        i = edge_y[itr_y][0]
        j = edge_y[itr_y][1]
        itr_y += 1
        if (i in V) == False and (j in V) == False:
            V.append(i)
            V.append(j)
            ans += dic_y[(i,j)]
        elif (i in V) == False:
            V.append(i)
            ans += dic_y[(i,j)]
        elif (j in V) == False:
            V.append(j)
            ans += dic_y[(i,j)]
print(ans)
