// Original C++ solution (slower):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

#include<cmath>

#include<iostream>

using namespace std;

const int inf=0x3f3f3f3f;

const int maxn=1e5+10;



struct edge{

    int u,v,w;

    edge(int _u=0,int _v=0,int _w=0):u(_u),v(_v),w(_w){

    }

    bool operator<(const edge & a)const{

        return w<a.w;

    }

}es[2*maxn];



struct point{

    int x,y,id;

    point(int _x=0,int _y=0,int _id=0):x(_x),y(_y),id(_id){}

} ps[maxn];



int n,cnt,f[maxn];



bool cmpx(point a,point b){

    return a.x<b.x;

}



bool cmpy(point a,point b){

    return a.y<b.y;

}



int findf(int a){

    return ((f[a]==-1)?a:f[a]=findf(f[a]));

}



int kruskal(){

    int i,ans=0,cc=0;

    memset(f,0xff,sizeof(f));

    sort(es,es+cnt);

    for(i=0;i<cnt;i++){

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

        int fu=findf(u),fv=findf(v);

        if(fu!=fv){

            ans+=w,++cc;

            f[fu]=fv;

        }

        if(cc==n-1)break;

    }

    if(cc<n-1)return -1;

    return ans;

}

int main(){

    int i,j,k,c,un,vn;

    scanf("%d",&n);

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

        scanf("%d%d",&ps[i].x,&ps[i].y);

        ps[i].id=i;

    }

    sort(ps+1,ps+n+1,cmpx);

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

        es[cnt++]=edge(ps[i].id,ps[i+1].id,(ps[i+1].x-ps[i].x));

    }

    sort(ps+1,ps+n+1,cmpy);

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

        es[cnt++]=edge(ps[i].id,ps[i+1].id,(ps[i+1].y-ps[i].y));

    }

    printf("%d\n",kruskal());

    return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



using namespace std;

typedef long long ll;

struct Point

{

    ll x,y;

    int id;

};

struct edge

{

    int a,b,w;

};

bool cmp1(Point a,Point b)

{

    return a.x<b.x;

}

bool cmp2(Point a,Point b)

{

    return a.y<b.y;

}

bool cmp3(edge a,edge b)

{

    return a.w<b.w;

}

int f[100005];

int bcj(int p)

{

    if(f[p]==p)

        return p;

    f[p]=bcj(f[p]);

    return f[p];

}

void join(int p,int q)

{

    int fp=bcj(p);

    int fq=bcj(q);

    if(fp!=fq)

        f[fp]=fq;

}



Point village[100005];

edge e[200005];

int main()

{

    int N;



    scanf("%d",&N);

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

    {

        f[i]=i;

    }

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

    {

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

        village[i].id=i;

    }



    int cnt=0;

    sort(village,village+N,cmp1);

    for(int i=0;i<N-1;i++)

    {

        e[cnt].a=village[i].id;

        e[cnt].b=village[i+1].id;

        e[cnt].w=village[i].x-village[i+1].x;

        if(e[cnt].w<0)

            e[cnt].w*=-1;

        cnt++;

    }

    sort(village,village+N,cmp2);

    for(int i=0;i<N-1;i++)

    {

        e[cnt].a=village[i].id;

        e[cnt].b=village[i+1].id;

        e[cnt].w=village[i].y-village[i+1].y;

        if(e[cnt].w<0)

            e[cnt].w*=-1;

        cnt++;

    }



    sort(e,e+cnt,cmp3);

    ll ans=0;

    int p=0;

    while(N-->1)

    {

        while(1)

        {



            if(bcj(e[p].a)!=bcj(e[p].b))

            {

                join(e[p].a,e[p].b);

                ans+=(ll)e[p].w;



                p++;

                break;

            }

            p++;

        }

    }

    printf("%lld",ans);







}


// Fastest known solution for this problem: