#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for(ll i = (a); i > (b); --i)
#define REP(i, n) for(ll i = 0; i < (n); ++i)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define mp make_pair
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CINT(...)                                                              \
    int __VA_ARGS__;                                                           \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    ll __VA_ARGS__;                                                            \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"

typedef long long ll;
typedef unsigned long long ul;
typedef vector<int> V;
typedef vector<ll> VL;
typedef pair<ll, ll> PL;
typedef priority_queue<ll> PQ;
typedef priority_queue<ll, V, greater<ll>> PQG;

const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18;

struct Edge {
    ll cost, from, to;
    bool operator<(const Edge &another) const { return cost < another.cost; };
    bool operator>(const Edge &another) const { return cost > another.cost; };
};
ll N;
vector<pair<ll, PL>> grid;
vector<Edge> es;

struct UnionFind {
    V par, rank;
    UnionFind(int size) { init(size); }
    void init(int size) {
        REP(i, size) {
            par.push_back(i);
            rank.push_back(0);
        }
    }
    int root(int x) {
        if(par[x] == x)
            return x;
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }
    void unite(int x, int y) {
        x = root(x);
        y = root(y);
        if(x == y) return;
        if(rank[x] < rank[y]) {
            swap(x, y);
        } else {
            if(rank[x] == rank[y]) rank[x]++;
        }
        par[y] = x;
    }
    bool same(int x, int y) { return root(x) == root(y); }
};

bool compx(pair<ll, PL> a, pair<ll, PL> b) {
    return a.second.first < b.second.first;
}
bool compy(pair<ll, PL> a, pair<ll, PL> b) {
    return a.second.second < b.second.second;
}

int main() {
    cin >> N;
    REP(i, N) grid.push_back({i, PL{IN(), IN()}});
    sort(All(grid), compx);
    REP(i, SZ(grid) - 1) {
        ll cost = grid[i + 1].second.first - grid[i].second.first;
        ll u = grid[i].first, v = grid[i + 1].first;
        es.push_back({cost, u, v});
    }
    sort(All(grid), compy);
    REP(i, SZ(grid) - 1) {
        ll cost = grid[i + 1].second.second - grid[i].second.second;
        ll u = grid[i].first, v = grid[i + 1].first;
        es.push_back({cost, u, v});
    }
    ll ans = 0;
    auto uf = UnionFind(N);
    sort(All(es));
    REP(i, SZ(es)) {
        Edge e = es[i];
        if(!uf.same(e.from, e.to)) {
            uf.unite(e.from, e.to);
            ans += e.cost;
        }
    }
    cout << ans << "\n";
}


Measured memory_bytes: 11260