// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

#define f first

#define s second

#define mp make_pair

typedef long long int ll;

const int N=1e5+5;

int n,vis[N];

pair< pair<int,int>, int >  p[N];

priority_queue < pair < int, pair<int,int> > > q;

ll ans;

int par(int x){

    return((vis[x]==-1||vis[x]==x)? x:par(vis[x]));

}

int main(){

    //cin.tie(NULL);

   // cout.tie(NULL);

    //ios_base::sync_with_stdio(false);

    memset(vis,-1,sizeof vis);

    cin>>n;

    for(int i=0;i<n;i++){

        cin>>p[i].f.f>>p[i].f.s;

        p[i].s=i;

    }

    sort(p,p+n);

    for(int i=1;i<n;i++)

        q.push( mp( -(p[i].f.f-p[i-1].f.f),mp(p[i].s,p[i-1].s)));



    for(int i=0;i<n;i++)

        swap(p[i].f.f,p[i].f.s);

    sort(p,p+n);

    for(int i=1;i<n;i++)

        q.push( mp(-(p[i].f.f-p[i-1].f.f),mp(p[i].s,p[i-1].s)));

    while(q.size()){

        pair< int, pair<int,int> > x=q.top();

        q.pop();

        //cout<<x.s.f<<" "<<vis[x.s.f]<<" "<<x.s.s<<" "<<vis[x.s.s]<<" "<<x.f<<'\n';

        vis[x.s.f]=par(x.s.f);

        vis[x.s.s]=par(x.s.s);

        if(vis[x.s.f]==vis[x.s.s])

            continue;

        ans+=-x.f;

        vis[vis[x.s.f]]=vis[x.s.s];

    }

    cout<<ans<<'\n';

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.4207192771733625x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cstring>

#include<cmath>

#include<vector>

#include<string>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<sstream>

#include<bitset>

using namespace std;

#define pii pair<int,int>

#define pdd pair<double,double>

#define mp make_pair

#define pb push_back

#define ALL(x)	x.begin(),x.end()

#define fst first

#define sec second

#define PQ priority_queue

typedef long long ll;

const double eps = 1e-6;



const int inf=0x3f3f3f3f;

const ll mod=1e9+7;

const double pi=acos(-1.0);

ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} 



const int maxn = 1e5+5;

int fa[maxn];

struct Node{

  ll x, y, id;

}st[maxn];

 

struct Edge{

  ll cost, p1, p2;

}edge[maxn*2];

 

int cmp1(Node a,Node b){ 

	return a.x<b.x; 

}

int cmp2(Node a,Node b){ 

	return a.y<b.y; 

}

int cmp3(Edge a,Edge b){ 

	return a.cost<b.cost; 

}

 

int find(int u){

  	if(fa[u]==u) 

  		return fa[u];

  	return fa[u]=find(fa[u]);

}

 

int main()

{

    int n;

    scanf("%d",&n);

    for(int i=1;i<=n;i++) {

		scanf("%lld%lld", &st[i].x, &st[i].y); 

		st[i].id=i; 

		fa[i]=i;

	}

    sort(st+1,st+1+n,cmp1);

    int cnt=0;

    for(int i=2;i<=n;i++){

        edge[++cnt].p1=st[i-1].id;

        edge[cnt].p2=st[i].id;

        edge[cnt].cost=st[i].x-st[i-1].x;

    }

    sort(st+1,st+1+n,cmp2);

    for(int i=2;i<=n;i++){

        edge[++cnt].p1=st[i-1].id;

        edge[cnt].p2=st[i].id;

        edge[cnt].cost=st[i].y-st[i-1].y;

    }

    sort(edge+1,edge+1+cnt,cmp3);

    ll ans=0;int num=0;

    for(int i=1;i<=cnt;i++){

        int x=find(edge[i].p1);

        int y=find(edge[i].p2);

        if(x==y) 

			continue;

        else{

            fa[x]=y;

            num++;

            ans+=(edge[i].cost);

        }

        if(num==n-1) break;

    }

    printf("%lld\n",ans);

    return 0;

}