/*
これを入れて実行
g++ code.cpp
./a.out
 */
 
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;
 
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}
 
ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}
 
ll nCr(ll n, ll r){
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
 
ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
 
//-----------------------ここから-----------
struct Edge{
    ll from;
    ll to;
    ll cost;
};

struct UnionFind{
    vector<int> parent;
    vector<int> s;
 
    UnionFind(int n){
        parent.resize(n);
        s.resize(n, 1);
        for(int i = 0; i < n; i++) parent[i] = i;
    }
 
    int root(int node){
        if(parent[node] == node){
            return node;
        } else {
            return parent[node] = root(parent[node]);
        }
    }
 
    void unite(int x, int y){
        int rootx = root(x);
        int rooty = root(y);
        if(rootx == rooty) return;
        if(s[rootx] < s[rooty]) swap(rootx, rooty);
        s[rootx] += s[rooty];
        parent[rooty] = rootx;
    }
 
    bool isSame(int x, int y){
        return root(x) == root(y);
    }
 
    int size(int x){
        return s[root(x)];
    }
};

ll kruskal(vector<Edge> &e, ll n){
    sort(e.begin(), e.end(), [](const Edge &a, const Edge &b){
        return a.cost < b.cost;
    });
    UnionFind uf(n);
    ll res = 0;
    for(int i = 0; i < e.size(); i++){
        if(!uf.isSame(e[i].from, e[i].to)){
            res += e[i].cost;
            uf.unite(e[i].from, e[i].to);
        }
    }
    return res;
}


int main(void){
    ll n;
    cin >> n;
    //vector<vector<Edge>> g(n);
    vector<Edge> e;
    vector<pair<pair<ll,ll>, ll>> place(n);
    vector<pair<pair<ll,ll>, ll>> placey(n);
    for(int i = 0; i < n; i++){
        ll x, y;
        cin >> x >> y;
        x--;
        y--;
        place[i] = make_pair(make_pair(x, y), i);
        placey[i] = make_pair(make_pair(y, x), i);
    }

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

    for(int i = 1; i < n; i++){
        ll lx, ly, li;
        lx = place[i - 1].first.first;
        ly = place[i - 1].first.second;
        li = place[i - 1].second;
        ll nx, ny, ni;
        nx = place[i].first.first;
        ny = place[i].first.second;
        ni = place[i].second;
        ll costx = abs(lx - nx);
        ll costy = abs(ly - ny);

        Edge tmp;
        tmp.cost = costx;
        tmp.from = li;
        tmp.to = ni;
        e.push_back(tmp);
        tmp.cost = costy;
        e.push_back(tmp);
    }

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

    for(int i = 1; i < n; i++){
        ll lx, ly, li;
        ly = placey[i - 1].first.first;
        lx = placey[i - 1].first.second;
        li = placey[i - 1].second;
        ll nx, ny, ni;
        ny = placey[i].first.first;
        nx = placey[i].first.second;
        ni = placey[i].second;
        ll costx = abs(lx - nx);
        ll costy = abs(ly - ny);

        Edge tmp;
        tmp.cost = costx;
        tmp.from = li;
        tmp.to = ni;
        e.push_back(tmp);
        tmp.cost = costy;
        e.push_back(tmp);
    }

    cout << kruskal(e, n) << endl;
}

Measured memory_bytes: 19180