#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 10000
#define r(i,n) for(int i=0;i<n;i++)
#define INF 1<<25
int n;
int mincost[MAX];
bool used[MAX];
pair<int,int> a[MAX];
int prim(){
  for(int i=0;i<n;i++)mincost[i]=INF,used[i]=false;
    mincost[0]=0;
  int res=0;
  while(1){
    int v=-1;
    for(int u=0;u<n;u++)
      if(!used[u]&&(v==-1||mincost[u]<mincost[v]))v=u;
    if(v==-1)break;
    used[v]=1;
    res+=mincost[v];
    for(int u=0;u<n;u++)mincost[u]=min(mincost[u],min(abs(a[v].first-a[u].first),abs(a[v].second-a[u].second)));
      //r(u,n)cout<<min(abs(a[v].first-a[v].first),abs(a[v].second-a[u].second))<<endl;
  }
return res;
}
main(){
  cin>>n;
  r(i,n){
    cin>>a[i].first>>a[i].second;
  }
  cout<<prim()<<endl;
}