#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;
const int maxn=1e7+5;
int cnt;
int par[maxn];
int n;

struct roads{
    ll x,y,id;
    bool operator <(const roads &other){
        return id<other.id;
    }
}road[maxn],road2[maxn];

int mind(roads q1,roads q2){
    return min(abs(q1.x-q2.x),abs(q1.y-q2.y));
}

void init(){
    for(int i=1;i<=n;i++)
        par[i]=i;
}

int find(int x){
    if(x==par[x])return x;
    return par[x]=find(par[x]);
}

void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return ;
    par[x]=y;
}

bool same(int x,int y){
    return find(x)==find(y);
}

ll kruskal(){
    ll sum=0;
    init();
    for(int i=0;i<cnt;i++){
        roads r=road2[i];
        if(r.id==0)unite(r.x,r.y);
        if(!same(r.x,r.y)){
            unite(r.x,r.y);
            sum+=r.id;
        }
    }
    return sum;
}

int main(){
    ios::sync_with_stdio(false);
    while(cin>>n){
//        maxq=0,minq=1e9+5;
        for(int i=1;i<=n;i++){
            cin>>road[i].x>>road[i].y;
//            if(maxq>max(road[i].x,road[i].y))
//                maxq=max(road[i].x,road[i].y);
//            if(minq<min(road[i].x,road[i].y))
//                minq=min(road[i].x,road[i].y);
            road[i].id=i;
        }
        cnt=0;
        for(int i=1;i<n;i++){
            for(int j=i+1;j<=n;j++){
                int mins=mind(road[i],road[j]);
                road2[cnt].x=road[i].id;
                road2[cnt].y=road[j].id;
                road2[cnt++].id=mins;
                road2[cnt].x=road[j].id;
                road2[cnt].y=road[i].id;
                road2[cnt++].id=mins;
            }
        }
//         for(int i=0;i<cnt;i++){
//            cout<<road2[i].x<<' '<<road2[i].y<<' '<<road2[i].id<<endl;
//        }
        sort(road2,road2+cnt);
        ll t=kruskal();
        cout<<t<<endl;
    }
    return 0;
}
