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

#ifndef ONLINE_JUDGE

#define debug(x) cout << #x << ": " << x << endl

#else

#define debug(x)

#endif

using namespace std;

typedef long long ll;

const int MAXN=4e5+7;

const int INF=0x3f3f3f3f;

const int MOD=1e9+7;



int fa[MAXN];

struct edge

{

    int u,v,w;

}e[MAXN];

int tot=0;

void addedge(int u,int v,int w)

{

    e[tot].u=u;

    e[tot].v=v;

    e[tot++].w=w;

}

bool cmp(edge a,edge b){return a.w<b.w;}



int Find(int x)

{

    if(fa[x]==-1) return x;

    else return fa[x]=Find(fa[x]);

}

ll kruskal(int n)

{

    memset(fa,-1,sizeof(fa));

    sort(e,e+tot,cmp);

    int cnt=0;

    ll ans=0;

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

    {

        int u=e[i].u,v=e[i].v,w=e[i].w;

        int t1=Find(u),t2=Find(v);

        if(t1!=t2)

        {

            ans+=w;

            fa[t1]=t2;

            cnt++;

        }

        if(cnt==n-1) return ans;

    }

    return -1;

}



struct node

{

    int x,y,id;

}p[MAXN];

bool cmp1(node a,node b)

{

    return a.x<b.x;

}

bool cmp2(node a,node b)

{

    return a.y<b.y;

}



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

    int n;

    cin>>n;

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

    {

        cin>>p[i].x>>p[i].y;

        p[i].id=i+1;

    }

    sort(p,p+n,cmp1);

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

    {

        if(i==0) addedge(p[i].id,p[i+1].id,abs(p[i+1].x-p[i].x));

        else if(i==n-1) addedge(p[i].id,p[i-1].id,abs(p[i-1].x-p[i].x));

        else

        {

            addedge(p[i].id,p[i+1].id,abs(p[i+1].x-p[i].x));

            addedge(p[i].id,p[i-1].id,abs(p[i-1].x-p[i].x));

        }

    }

    sort(p,p+n,cmp2);

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

    {

        if(i==0) addedge(p[i].id,p[i+1].id,abs(p[i+1].y-p[i].y));

        else if(i==n-1) addedge(p[i].id,p[i-1].id,abs(p[i-1].y-p[i].y));

        else

        {

            addedge(p[i].id,p[i+1].id,abs(p[i+1].y-p[i].y));

            addedge(p[i].id,p[i-1].id,abs(p[i-1].y-p[i].y));

        }

    }

    cout<<kruskal(n)<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.5826432206878227x):
#include <bits/stdc++.h>

#define EPS 1e-9

#define PI acos(-1)

using namespace std;

typedef long long ll;

const int maxn = 1e5+100;

int fa[maxn];



struct point

{

    int x,y,id;

}p[maxn];



struct costt

{

    int p1,p2,cost;

}l[maxn*4];



int fin(int a)

{

    if(fa[a]==a) return a;

    else return fa[a]=fin(fa[a]);

}



bool cmp1(const point &a,const point &b)

{

    return a.x<b.x;

}



bool cmp2(const point &a,const point &b)

{

    return a.y<b.y;

}



bool cmp3(const costt &a,const costt &b)

{

    return a.cost<b.cost;

}



int main()

{

    ios::sync_with_stdio(false);

    int n;

    cin>>n;

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

    {

        cin>>p[i].x>>p[i].y;

        p[i].id = i;

        fa[i] = i;

    }

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

    int tot = 0;

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

    {

        l[tot].p1 = p[i].id;

        l[tot].p2 = p[i+1].id;

        l[tot].cost = abs(p[i].x-p[i+1].x);

        tot++;

    }

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

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

    {

        l[tot].p1 = p[i].id;

        l[tot].p2 = p[i+1].id;

        l[tot].cost = abs(p[i].y-p[i+1].y);

        tot++;

    }

    sort(l,l+tot,cmp3);

    ll ans = 0;

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

    {

        int fa_p1 = fin(l[i].p1);

        int fa_p2 = fin(l[i].p2);

        if(fa_p1==fa_p2) continue;

        else

        {

            fa[fa_p1] = fa_p2;

            ans += l[i].cost;

        }

    }

    cout<<ans<<"\n";

}