#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;

const int maxn=1e5+5;
int fa[maxn];
int tot;
struct way{
    int u,v;
	long long w;
}edge[maxn*4];
struct node
{
	int x,y,id;
}a[maxn];
int find(int x){return fa[x]==x?fa[x]:fa[x]=find(fa[x]);}
void link(int x,int y)
{
    int tx=find(x),ty=find(y);
    fa[ty]=tx;
}
bool cmp(way a,way b){return a.w<b.w;}
bool cmp1(node a,node b){return a.x<b.x;}
bool cmp2(node a,node b){return a.y<b.y;}
int main()
{
    int m,n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
    	a[i].id=i;
    	scanf("%d %d",&a[i].x,&a[i].y);
	}
	sort(a+1,a+1+n,cmp1);
	int cnt=0;
    for(int i=1;i<n;i++)
    {
    	cnt++;
    	edge[cnt].u=a[i].id;
    	edge[cnt].v=a[i+1].id;
    	edge[cnt].w=(long long)(a[i+1].x-a[i].x);
    	cnt++;
    	edge[cnt].u=a[i+1].id;
    	edge[cnt].v=a[i].id;
    	edge[cnt].w=(long long)(a[i+1].x-a[i].x);
	}
	sort(a+1,a+1+n,cmp2);
	for(int i=1;i<n;i++)
    {
    	cnt++;
    	edge[cnt].u=a[i].id;
    	edge[cnt].v=a[i+1].id;
    	edge[cnt].w=(long long)(a[i+1].y-a[i].y);
    	cnt++;
    	edge[cnt].u=a[i+1].id;
    	edge[cnt].v=a[i].id;
    	edge[cnt].w=(long long)(a[i+1].y-a[i].y);
	}
    for(int i=1;i<=n;i++)    fa[i]=i;
    sort(edge+1,edge+cnt+1,cmp);
    for(int i=1;i<=cnt;i++)
    {
        if(find(edge[i].u)!=find(edge[i].v))
        {
            link(edge[i].u,edge[i].v);
            tot=tot+edge[i].w; 
        }
    }
    cout<<tot<<endl;
    return 0;
}
#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define MAX 2000005
#define MOD 1000000007
#define MOD2 998244353
#define pb push_back
#define mp make_pair
#define pii pair<ll,ll>
#define vi vector<ll>
#define vp vector<pii>
#define all(v) v.begin(),v.end()
#define sz(x) (ll)x.size()
#define F first
#define S second
#define FOR(i,a,b) for(ll i=a;i<b;++i)
#define ROF(i,a,b) for(ll i=a;i>=b;--i)
#define trace(x) cerr<<#x<<": "<<x<<'\n';
typedef long long ll;
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

ll mexp(ll a, ll b){
    ll ans=1;
    a%=MOD;
    while(b){
        if(b&1) ans=ans*a%MOD;
        b>>=1;
        a=a*a%MOD;
    }
    return ans;
}

const int N = 100005;

bool cmp1(pair<int,pair<int,int>>a, pair<int,pair<int,int>>b){
    return a.S.F<b.S.F;
}

bool cmp2(pair<int,pair<int,int>>a, pair<int,pair<int,int>>b){
    return a.S.S<b.S.S;
}

pair<int,pair<int,int>>c[N];
int par[N],siz[N];

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

void un(int a,int b){
    a=find(a);
    b=find(b);
    if(siz[a]<siz[b]){
        par[a]=par[b];
        siz[b]+=siz[a];
    }
    else{
        par[b]=par[a];
        siz[a]+=siz[b];
    }
}

int main(){
    IOS

    int n;
    cin>>n;
    FOR(i,1,n+1){
        par[i]=i;
        siz[i]=1;
        c[i].F=i;
        cin>>c[i].S.F>>c[i].S.S;
    }

    vector<pair<int,pair<int,int>>>v;

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

    FOR(i,1,n){
        v.pb(mp(c[i+1].S.F-c[i].S.F,mp(c[i].F,c[i+1].F)));
    }
    sort(c+1,c+n+1,cmp2);
    FOR(i,1,n){
        v.pb(mp(c[i+1].S.S-c[i].S.S,mp(c[i].F,c[i+1].F)));
    }
    sort(all(v));

    int cnt=0;
    int i=0;
    ll ans=0;
    while(cnt<n-1){
        int x=find(v[i].S.F),y=find(v[i].S.S);
        if(x==y){
            i++;
            continue;
        }
        un(x,y);
        ans+=v[i].F;
        i++;
        cnt++;
    }
    cout<<ans;

    cerr<<"Time: "<<1.0/CLOCKS_PER_SEC<<" ms"<<endl;
    return 0;
}