import numpy as np

N=int(input())
pos_mat=np.zeros((N,2))
for i in range(N):
    buf=input().split()
    pos_mat[i,0]=int(buf[0])
    pos_mat[i,1]=int(buf[1])
    
cost_mat=np.zeros((N,N))
for u in range(N):
    for v in range(u+1, N):
        cost_mat[u,v]=min(abs(pos_mat[u, 0]-pos_mat[v,0]), abs(pos_mat[u, 1]-pos_mat[v,1]))

        
minCost_arr=np.array([float("inf") for _ in range(N)])
used_arr=np.zeros(N, dtype=bool)
minCost_arr[0]=0

totalCost=0
while np.sum(used_arr)<N:
    
    idx=np.arange(N)[~used_arr]
    u=idx[np.argmin(minCost_arr[idx])]
    
    totalCost+=minCost_arr[u]
    used_arr[u]=True
    for v in range(N):
        edgeCost=cost_mat[min(u,v), max(u,v)]
        minCost_arr[v]=min(minCost_arr[v], edgeCost)
print(int(totalCost))