// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>



using namespace std;

const int maxN=1e5+5;



int fa[maxN];



struct node

{

    int x,y,id;

}a[maxN],b[maxN];



struct edge

{

    int f,t,w;

}e[maxN*2];



bool cmp1(node x1,node x2)

{

    return x1.x<x2.x;

}



bool cmp2(node x1,node x2)

{

    return x1.y<x2.y;

}



bool cmp3(edge x1,edge x2)

{

    return x1.w<x2.w;

}



int find(int x)

{

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

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

}



int uni(int f,int t)

{

    f=find(f);t=find(t);

    if(f==t)return 0;

    fa[f]=t;return 1;

}



int main()

{

    int k,n,cnt=0,now=0;

    long long ans=0;

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

    scanf("%d",&n);

    for(k=0;k<n;k++)

    {

        scanf("%d%d",&a[k].x,&a[k].y);

        a[k].id=k;b[k].id=k;

        b[k].x=a[k].x;

        b[k].y=a[k].y;

    }

    sort(a,a+n,cmp1);

    sort(b,b+n,cmp2);

    for(k=1;k<n;k++)

    {

        e[cnt].f=a[k-1].id;

        e[cnt].t=a[k].id;

        e[cnt].w=a[k].x-a[k-1].x;

        cnt++;

    }

    for(k=1;k<n;k++)

    {

        e[cnt].f=b[k-1].id;

        e[cnt].t=b[k].id;

        e[cnt].w=b[k].y-b[k-1].y;

        cnt++;

    }

    sort(e,e+cnt,cmp3);

    for(k=0;k<cnt;k++)

    {

        if(uni(e[k].f,e[k].t))

        {

            ans+=e[k].w;

            now++;

        }

        if(now==n-1)break;

    }

    cout<<ans;

    return 0;

}


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

#include <algorithm>

#include <cstdio>

#define rush() int T;cin>>T;while(T--)

#define go(a) while(cin>>a)

#define ms(a,b) memset(a,b,sizeof a)

#define E 1e-8

using namespace std;

typedef long long ll;

const int N=1e5+5;



    int n,m,t;

    int fa[N];

    int i,j,k;

struct point

{

    int x,y,id;

}p[N];

struct Edge

{

    int u,v,valu;

    bool const operator<(const Edge &b)

    const{

        return valu<b.valu;

    }

}edge[N];

int Find(int son)

{

    return son==fa[son] ? son : fa[son]=Find(fa[son]);

}

bool Union(int x,int y)

{

    x=Find(x),y=Find(y);

    if(x==y) return 1;

    else{

        fa[y]=x;

        return 0;

    }

}

bool cmpx(point &a,point &b){

    return a.x<b.x;

}

bool cmpy(point &a,point &b){

    return a.y<b.y;

}

int main()

{

    cin.tie(0);istream::sync_with_stdio(false);

    while(cin>>n){

        for(i=1;i<=n;i++) fa[i]=i;



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

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

            p[i].id=i;

        }



        int num=0;

        sort(p+1,p+1+n,cmpx);//尽量以x之间的距离为经费建路

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

            edge[++num].u=p[i].id;

            edge[num].v=p[i+1].id;

            edge[num].valu=min(abs(p[i].x-p[i+1].x),abs(p[i].y-p[i+1].y));

        }

        sort(p+1,p+1+n,cmpy);//尽量以y之间的距离为经费建路

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

            edge[++num].u=p[i].id;

            edge[num].v=p[i+1].id;

            edge[num].valu=min(abs(p[i].x-p[i+1].x),abs(p[i].y-p[i+1].y));

        }



        stable_sort(edge+1,edge+1+num);

        int ans=0,cnt=0;

        for(i=1;i<=num;i++){

            if(0==Union(edge[i].u,edge[i].v)){//若u，v两点还未连接

                ans+=edge[i].valu;

                cnt++;

            }

            if(cnt==n-1) break;//建路数达到联通所有城市

        }

        cout<<ans<<endl;

    }

}