#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
ll mod = 1000000007;

class UnionFind {
  private:
    int num;
    vector<int> par;
    vector<int> sizes;
    vector<int> rank;

  public:
    UnionFind(int n) { init(n); }

    ~UnionFind() {}

    int init(int n) {
        num = n;
        par.resize(n);
        sizes.resize(n);
        rank.resize(n);
        for (int i = 0; i < n; i++) {
            par[i] = i;
            sizes[i] = 1;
            rank[i] = 0;
        }
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = root(par[x]);
        }
    }

    void unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry)
            return;
        if (rank[rx] < rank[ry]) {
            par[rx] = ry;
        } else {
            par[ry] = rx;
            if (rank[rx] == rank[ry]) {
                rank[rx]++;
            }
        }
        int temp = sizes[rx] + sizes[ry];
        sizes[rx] = temp;
        sizes[ry] = temp;
    }

    int get_size(int x) { return sizes[root(x)]; }

    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};
class edge {
  public:
    int u, v, cost;
    edge() {
        u = 0;
        v = 0;
        cost = 0;
    }
    edge(int a, int b, int c) {
        u = a;
        v = b;
        cost = c;
    }
};

bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }

int main() {
source:
    ll n;
    cin >> n;
    ll x[n], y[n];
    vector<pair<ll, ll>> v;
    rep(i, n) {
        cin >> x[i] >> y[i];
        v.push_back(make_pair(x[i], y[i]));
    }
    ll index_array[n];
    rep(i, n) { index_array[i] = i; }

    vector<edge> es;

    sort(v.begin(), v.end());
    sort(index_array, index_array + n, [&](ll const &a, ll const &b) {
        if (v[a].first != v[b].first) {
            return v[a].first < v[b].first;
        } else {
            return v[a].second < v[b].second;
        }
    });
    repd(i, 1, n) {
        int cost =
            min(abs(v[index_array[i]].first - v[index_array[i - 1]].first),
                abs(v[index_array[i]].second - v[index_array[i - 1]].second));
        es.push_back(edge(index_array[i], index_array[i - 1], cost));
    }

    sort(index_array, index_array + n, [&](ll const &a, ll const &b) {
        if (v[a].second != v[b].second) {
            return v[a].second < v[b].second;
        } else {
            return v[a].first < v[b].first;
        }
    });
    repd(i, 1, n) {
        int cost =
            min(abs(v[index_array[i]].first - v[index_array[i - 1]].first),
                abs(v[index_array[i]].second - v[index_array[i - 1]].second));
        es.push_back(edge(index_array[i], index_array[i - 1], cost));
    }

    sort(es.begin(), es.end(), comp);

    ll result = 0;
    UnionFind u(n);
    for (auto e : es) {
        if (!u.same(e.u, e.v)) {
            u.unite(e.u, e.v);
            result += e.cost;
        }
    }

    cout << result << endl;
    return 0;
}



Measured memory_bytes: 10224