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

using namespace std;

#define f first

#define s second

#define mp make_pair

typedef long long int ll;

const int N=1e5+5;

int n,vis[N];

pair< pair<int,int>, int >  p[N];

priority_queue < pair < int, pair<int,int> > > q;

ll ans;

int par(int x){

    return((vis[x]==-1||vis[x]==x)? x:par(vis[x]));

}

int main(){

    //cin.tie(NULL);

   // cout.tie(NULL);

    //ios_base::sync_with_stdio(false);

    memset(vis,-1,sizeof vis);

    cin>>n;

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

        cin>>p[i].f.f>>p[i].f.s;

        p[i].s=i;

    }

    sort(p,p+n);

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

        q.push( mp( -(p[i].f.f-p[i-1].f.f),mp(p[i].s,p[i-1].s)));



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

        swap(p[i].f.f,p[i].f.s);

    sort(p,p+n);

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

        q.push( mp(-(p[i].f.f-p[i-1].f.f),mp(p[i].s,p[i-1].s)));

    while(q.size()){

        pair< int, pair<int,int> > x=q.top();

        q.pop();

        //cout<<x.s.f<<" "<<vis[x.s.f]<<" "<<x.s.s<<" "<<vis[x.s.s]<<" "<<x.f<<'\n';

        vis[x.s.f]=par(x.s.f);

        vis[x.s.s]=par(x.s.s);

        if(vis[x.s.f]==vis[x.s.s])

            continue;

        ans+=-x.f;

        vis[vis[x.s.f]]=vis[x.s.s];

    }

    cout<<ans<<'\n';

    return 0;

}


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

#include <cstring>

#include <cstdlib>

#include <cstdio>

#include <algorithm>

#include <cmath>

#include <iomanip>

#include <vector>

using namespace std;

const int maxn = 1e6+100;

const int MOD = 1e9+7;

const double Pi = 3.141592654;

typedef long long ll;

typedef unsigned long long ull;



struct node

{

    ll x;

    ll y;

    int id;

} pos[maxn];



bool cmp1(node a, node b)

{

    if(a.x == b.x)

        return a.y < b.y;

    return a.x < b.x;

}



bool cmp2(node a, node b)

{

    if(a.y == b.y)

        return a.x < b.x;

    return a.y < b.y;

}



struct edge

{

    int u;

    int v;

    ll w;

} g[maxn];



bool cmp(edge a, edge b)

{

    return a.w < b.w;

}



ll pre[maxn];



int Find(int x)

{

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

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

}



void unit(int x,int y)

{

    x = Find(x);

	y = Find(y);

    if(x != y)

    	pre[y] = x;

}



bool same(int x, int y)

{

    return Find(x) == Find(y);

}



int main()

{

    int n;

    cin >> n;

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

    {

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

        pos[i].id = i;

    }

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

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

    {

        g[i].u = g[i+n].v = pos[i].id;

        g[i].v = g[i+n].u = pos[i+1].id;

        g[i].w = g[i+n].w = abs(pos[i].x - pos[i+1].x);

    }

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

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

    {

        g[i+2*n].u = g[i+3*n].v = pos[i].id;

        g[i+2*n].v = g[i+3*n].u = pos[i+1].id;

        g[i+2*n].w = g[i+3*n].w = abs(pos[i].y-pos[i+1].y);

    }

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

    {

        pre[i] = i;

    }

    sort(g+1, g+1+4*n, cmp);

    int sum = 0, cnt = 0;

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

    {

        if(cnt == n - 1)

            break;

        if(!same(g[i].u, g[i].v))

        {

            unit(g[i].u, g[i].v);

            sum += g[i].w;

            cnt ++;

        }

    }

    cout << sum << endl;

}