#include<algorithm>
#include<complex>
#include<ctype.h>
#include<iomanip>
#include<iostream>
#include<map>
#include<math.h>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<stdio.h>
#include<string>
#include<string>
#include<vector>

using namespace std;
typedef long long ll;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

struct Point{
    ll x;
    ll y;
    ll id;
    Point(ll x, ll y, ll id): x(x), y(y), id(id) {}

    static bool sortByX(Point a, Point b){
        if(a.x == b.x){
            return a.y < a.y;
        }else{
            return a.x < b.x;
        }
    }

    static bool sortByY(Point a, Point b){
        if(a.y == b.y){
            return a.x < a.x;
        }else{
            return a.y < b.y;
        }
    }
};

struct Edge{
    ll from;
    ll to;
    ll cost;

    static bool sortByCost(Edge a, Edge b){
        return a.cost < b.cost;
    }

    string ToString(){
        return "[edge]" + to_string(from) + " " + to_string(to) + " " + to_string(cost);
    }
};

const int N_MAX = 100010;
int parent[N_MAX] = {};
vector<Point> P;
vector<Edge> edgeList;

ll findRoot(ll x){
    if(parent[x] == x){
        return x;
    }else{
        auto root = findRoot(parent[x]);
        parent[x] = root; // path compression
        return root;
    }
}

void _union(ll a, ll b){
    auto rootA = findRoot(a);
    auto rootB = findRoot(b);
 
    if(rootA == rootB){
        return;
    }else{
        parent[a] = b;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll N;
    cin >> N;

    P.reserve(N);

    FOR(i, 0, N){
        ll x, y;
        cin >> x >> y;
        Point p = Point(x, y, i);
        P.push_back(p);
    }

    // p("[point]");
    // for(auto p : P){
    //     cout << p.id << " " << p.x << " " << p.y << endl;
    // }

    sort(ALL(P), Point::sortByX);
    FOR(i, 0, N-1){
        Edge edge;
        edge.from = P[i].id;
        edge.to = P[i+1].id;
        edge.cost = min(abs(P[i].x - P[i+1].x), abs(P[i].y - P[i+1].y));
        edgeList.push_back(edge);
    }

    sort(ALL(P), Point::sortByY);
    FOR(i, 0, N-1){
        Edge edge;
        edge.from = P[i].id;
        edge.to = P[i+1].id;
        edge.cost = min(abs(P[i].x - P[i+1].x), abs(P[i].y - P[i+1].y));
        edgeList.push_back(edge);
    }

    sort(ALL(edgeList), Edge::sortByCost);

    // make MST by kruskal
    FOR(i, 0, N_MAX){
        parent[i] = i;
    }

    ll cost_sum = 0;

    // p("[edge]");
    // for(auto edge : edgeList){
    //     cout << edge.from << " " << edge.to << " " << edge.cost << endl;
    // }

    // 短いエッジから使うか検討
    for(auto edge : edgeList){
        // p("[parent]");
        // FOR(i, 0, N){
        //     p2(i, parent[i]);
        // }

        //p(edge.ToString());

        if(findRoot(edge.from) == findRoot(edge.to)){
            // skip
        }
        else{
            //cout << "つなぐ " << edge.from << " " << edge.to << endl;
            // つなぐ
            _union(findRoot(edge.from), findRoot(edge.to));
            cost_sum += edge.cost;

            if(edge.cost>0){
                //cout << "cost=1 " << edge.from << " " << edge.to << endl;
                // p(findRoot(edge.from));
                // p(findRoot(edge.to));
            }
        }
    }

    //p("answer");
    p(cost_sum);
    
    return 0;
}

Measured memory_bytes: 10864