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

using namespace std;



#define ENGZ ios::sync_with_stdio(0);cin.tie(0);ios_base::sync_with_stdio(0);

#define sfi(x) scanf("%d" , &x)

#define sfi2(x, y) scanf("%d%d" , &x,&y)

#define sfl2(x, y) scanf("%I64d%I64d" , &x,&y)

#define sfl(x) scanf("%I64d" , &x)

#define sfl3(x, y, z) scanf("%I64d%I64d%I64d", &x, &y, &z)

#define sfd(x) scanf("%lf", &x)

#define sfd2(x, y) scanf("%lf", &x, &y)

#define sfs(x) scanf("%s", &x)

#define testv(x) cerr << x << ' '

#define wtf(s) freopen((s), "w", stdout)

#define rff(s) freopen((s), "r", stdin)

#define test cerr << "here"

#define fst(x) cout << fixed << setprecision(x)

#define mod 1000000007

#define pi (2 * acos(0))

typedef long double ld;

typedef long long ll;



int par[200005], rnk[200005];



int findp(int x)

{

    if (par[x] == x)

        return x;

    return par[x] = findp(par[x]);

}



bool iscon(int x, int y)

{

    if (findp(x) == findp(y))

        return true;

    return false;

}



void con(int x, int y)

{

    int px = findp(x), py = findp(y);

    if (rnk[px] > rnk[py])

        par[py] = px;

    else if (rnk[px] < rnk[py])

        par[px] = py;

    else

        par[px] = py, rnk[px]++;

}





    pair<pair<ll, ll>, int> a[100005], b[100005];

int main()

{

    ENGZ

    int n;

    ll ans = 0;

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

        par[i] = i;

    cin >> n;

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

    {

        cin >> a[i].first.first >> a[i].first.second;

        b[i].first.first = a[i].first.second;

        b[i].first.second = a[i].first.first;

        a[i].second = b[i].second = i;

    }

    sort(a, a + n);

    sort(b, b + n);

    map<ll, vector<pair<int, int>>> mp;

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

    {

        mp[abs(a[i].first.first - a[i + 1].first.first)].push_back({a[i].second, a[i + 1].second});

        mp[abs(b[i].first.first - b[i + 1].first.first)].push_back({b[i].second, b[i + 1].second});

    }

    for (auto it: mp)

    {

        for (auto i: it.second)

        {

            if (!iscon(i.first, i.second))

                con(i.first, i.second), ans += it.first;

        }

    }

    cout << ans;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4023574559625107x):
#define _USE_MATH_DEFIMES

#include <algorithm>

#include <array>

#include <bitset>

#include <cassert>

#include <cctype>

#include <climits>

#include <clocale>

#include <cmath>

#include <complex>

#include <cstdio>

#include <cstdlib>

#include <ctime>

#include <deque>

#include <fstream>

#include <functional>

#include <iomanip>

#include <iostream>

#include <iterator>

#include <list>

#include <map>

#include <numeric>

#include <queue>

#include <regex>

#include <set>

#include <sstream>

#include <stack>

#include <string>

#include <tuple>

#include <unordered_map>

#include <unordered_set>

#include <utility>

#include <vector>



const int MOD = 1'000'000'007;

const int INF = 1'000'000'000; //1e9

const int NIL = -1;

const long long LINF = 1'000'000'000'000'000'000; // 1e18

const long double EPS = 1E-10;



template<class T, class S> inline bool chmax(T &a, const S &b){

    if(a < b){

        a = b; return true;

    }

    return false;

}

template<class T, class S> inline bool chmin(T &a, const S &b){

    if(b < a){

        a = b; return true;

    }

    return false;

}





struct edge{

    int to, cost;

    edge(int To, int Cost): to(To), cost(Cost){};

};





long long prim(std::vector<std::vector<edge>>& G){

    int V(G.size());

    long long res(0);

    //std::pair<int, int> first: 距離　second: 頂点

    std::priority_queue<std::pair<int, int>,

                        std::vector<std::pair<int, int>>,

                        std::greater<std::pair<int, int>>> que;

    std::vector<int> d(V, INF);

    d[0] = 0;

    que.push({0, 0});



    while(!que.empty()){

        std::pair<int, int> p = que.top(); que.pop();

        int v(p.second);

        if(d[v] < p.first) continue;

        d[v] = 0;

        res += p.first;

        for(edge &e : G[v]){

            if(d[e.to] > e.cost){

                d[e.to] = e.cost;

                que.push({d[e.to], e.to});

            }

        }

    }

    return res;

}



int main(){

    int N; std::cin >> N;

    std::vector<std::vector<edge>> G(N);

    //x,y,id と y,x,id

    std::vector<std::pair<std::pair<int, int>, int>> px(N), py(N);

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

        px[i].second = py[i].second = i;

        std::cin >> px[i].first.first >> px[i].first.second;

        py[i].first.first = px[i].first.second;

        py[i].first.second = px[i].first.first;

    }

    std::sort(std::begin(px), std::end(px));

    std::sort(std::begin(py), std::end(py));

    for(int i(0), i_len(N-1); i < i_len; ++i){

        G[px[i].second].emplace_back(px[i+1].second, 

                                     std::abs(px[i].first.first - px[i+1].first.first));

        G[px[i+1].second].emplace_back(px[i].second, 

                                     std::abs(px[i].first.first - px[i+1].first.first));

        G[py[i].second].emplace_back(py[i+1].second, 

                                     std::abs(py[i].first.first - py[i+1].first.first));

        G[py[i+1].second].emplace_back(py[i].second, 

                                     std::abs(py[i].first.first - py[i+1].first.first));

    }

    std::cout << prim(G) << std::endl;

    return 0;

}