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

using namespace std;

const long long llINF = 9223372036854775807;

const int INF = 2147483647;

const int maxn = 1e5 + 7;

const int maxm = 1e6 + 7;

const int mod = 1e9 + 7;

const double pi = acos(-1.0);

const double eps = 1e-10;

int n;

int fa[maxn];

struct node

{

    long long u, v, w;

};

vector<node> edge;

bool operator<(node a, node b)

{

    return a.w < b.w;

}

struct node2

{

    long long x, y, id;

} city[maxn];



bool cmp1(node2 a, node2 b)

{

    return a.x < b.x;

}



bool cmp2(node2 a, node2 b)

{

    return a.y < b.y;

}

void init()

{

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

        fa[i] = i;

}

int get(int x)

{

    return fa[x] == x ? x : fa[x] = get(fa[x]);

}

long long ans;

int main(int argc, char const *argv[])

{

    init();

    scanf("%d", &n);

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

    {

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

        city[i].id = i;

    }

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

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

        edge.push_back(node{city[i].id, city[i + 1].id, abs(city[i].x - city[i + 1].x)});



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

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

        edge.push_back(node{city[i].id, city[i + 1].id, abs(city[i].y - city[i + 1].y)});



    sort(edge.begin(),edge.end());



    for(int i=0;i<edge.size();i++)

    {

        int x=get(edge[i].u);

        int y=get(edge[i].v);

        if(x==y)

            continue;

        fa[x]=y;

        ans+=edge[i].w;

    }

    

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

    return 0;

}

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

using namespace std;

typedef long long ll;



#define ls rt<<1

#define rs rt<<1|1

typedef pair<int,int> pii;

const int maxn = 2e5 + 10;

const int maxm = 1e6 + 10 ;

const int inf=0x3f3f3f3f;

const ll INF = 0x3f3f3f3f3f3f3f3fLL;

const int st=(1<<20)-1;

const int mod = 1e9+7;

const double eps = 1e-5;

const int vec[2][2]={1,0,0,1};



inline int read(){

    int x=0,f=1;

    char ch=getchar();

    while(ch<'0'||ch>'9'){

        if(ch=='-')

            f=-1;

        ch=getchar();

    }

    while(ch>='0'&&ch<='9'){

        x=(x<<1)+(x<<3)+(ch^48);

        ch=getchar();

    }

    return x*f;

}



inline ll sv(ll x) {

    ll ans=1;

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

        ans*=i;

        ans%=mod;

    }

    return ans;

}



struct node {

    int x,y,id;

}a[maxn];

struct edge{

    int x,y,w;

    bool operator < (const edge &b)const{

        return w<b.w;

    }

}e[maxn<<2];

inline bool cmp1(const node &x,const node &y){

    return x.x<y.x;

}

inline bool cmp2(const node &x,const node &y){

    return x.y<y.y;

}

int f[maxn];

inline int fa(int x) {

    if(f[x]==x) return x;

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

}

int main() {

#ifdef LOCAL

    freopen("input.txt","r",stdin);

#endif // LOCAL

//    ios::sync_with_stdio(false);

//    cin.tie(0);

    int n;

    n=read();

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

        a[i].x=read();

        a[i].y=read();

        a[i].id=i;

    }

    sort(a,a+n,cmp1);

    int m=0;

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

        e[m].x=a[i].id; e[m].y=a[i-1].id;

        e[m].w=a[i].x-a[i-1].x;

        m++;

    }

    sort(a,a+n,cmp2);

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

        e[m].x=a[i].id; e[m].y=a[i-1].id;

        e[m].w=a[i].y-a[i-1].y;

        m++;

    }

    sort(e,e+m);

    for(int i=0;i<n;i++) f[i]=i;

    ll ans=0;

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

        int fx=fa(e[i].x),fy=fa(e[i].y);

        if(fx!=fy) {

            f[fx]=fy;

            ans+=e[i].w;

        }

    }

    cout<<ans;

    return 0;

}

//校园网真tm卡