// 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.3788300348745477x):
#include<bits/stdc++.h>

using namespace std;



// Shortcut



#define int long long

#define endl '\n'

#define eb emplace_back

#define pb push_back

#define pob pop_back

#define mp make_pair

#define upb upper_bound

#define lwb lower_bound

#define fi first

#define se second

#define For(i, l, r) for (int i = l; i < r; i++)

#define ForE(i, l, r) for (int i = l; i <= r; i++)

#define Ford(i, r, l) for (int i = r; i > l; i--)

#define FordE(i, r, l) for (int i = r; i >= l; i--)

#define Fora(i, a) for (auto i : a)



// I/O & Debug



#define PrintV(a) for (int iiii = 0; iiii < a.size(); iiii++) cout << a[iiii] << ' ';

#define PrintVl(a) for (int iiii = 0; iiii < a.size(); iiii++) cout << a[iiii] << endl;

#define PrintA(a, n) for (int iiii = 0; iiii < n; iiii++) cout << a[iiii] << ' ';

#define PrintAl(a, n) for (int iiii = 0; iiii < n; iiii++) cout << a[iiii] << endl;

#define Ptest(x) return cout << x, 0;

#define gl(s) getline(cin, s);

#define setpre(x) fixed << setprecision(x)

/*

#define debug(args...){ string _sDEB = #args; replace(_sDEB.begin(), _sDEB.end(), ',', ' '); stringstream _ssDEB(_sDEB); istream_iterator<string> _itDEB(_ssDEB); DEB(_itDEB, args); }

void DEB(istream_iterator<string> it) {}

template<typename T, typename... Args>

void DEB(istream_iterator<string> it, T a, Args... args){

    cout << *it << " = " << a << endl;

    DEB(++it, args...);

}

*/



// Functions



//#define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')

#define bend(a) a.begin(), a.end()

#define rbend(a) a.rbegin(), a.rend()

#define mset(a) memset(a, 0, sizeof(a));

//mt19937 rando(chrono::steady_clock::now().time_since_epoch().count());



// Data Structure



#define pque priority_queue

#define mts multiset

typedef long long ll;

typedef long double ld;

typedef vector <int> vi;

typedef vector <ll> vll;

typedef vector <ld> vld;

typedef vector <string> vs;

typedef pair <int, int> pii;

typedef pair <ll, ll> pll;

typedef vector <vi > vvi;

typedef vector <vll > vvll;

typedef vector <pii > vpii;

typedef vector <pll > vpll;



const int N = 1e5 + 5, mod = 1e9 + 7, inf = 1e18 + 5;



int n;



struct node{

    int u, v, w;

    bool operator< (const node &a) const{

        return w < a.w;

    }

} e[N * 2];



struct point{

    int w, id;

    bool operator< (const point &a) const{

        return w < a.w;

    }

} x[N], y[N];



int par[N];



int get(int x){

    while (x != par[x]){

        x = par[x] = par[par[x]];

    }

    return x;

}



signed main(){

    ios_base::sync_with_stdio(0);

    cin.tie(0); cout.tie(0);

    cin >> n;

    ForE(i, 1, n){

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

        x[i].id = i;

        y[i].id = i;

        par[i] = i;

    }

    sort(x + 1, x + n + 1);

    sort(y + 1, y + n + 1);

    int sz = 0, sum = 0, cnt = 1;

    For(i, 1, n){

        e[++sz] = (node){x[i].id, x[i + 1].id, abs(x[i].w - x[i + 1].w)};

    }

    For(i, 1, n){

        e[++sz] = (node){y[i].id, y[i + 1].id, abs(y[i].w - y[i + 1].w)};

    }

    sort(e + 1, e + sz + 1);

    ForE(i, 1, sz){

        int pu = get(e[i].u), pv = get(e[i].v);

        if (pu == pv){

            continue;

        }

        sum += e[i].w;

        par[pu] = pv;

        if (++cnt == n){

            break;

        }

    }

    cout << sum;

}



/*

==================================+

INPUT:                            |

------------------------------    |



------------------------------    |

==================================+

OUTPUT:                           |

------------------------------    |



------------------------------    |

==================================+

*/