#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;

struct UnionFind {
    static const int Max = 100010;
    int par[Max];
    int rank[Max];

    void init(int n) {
        for (int i = 0; i < n; ++i) {
            par[i] = i;
            rank[i] = 0;
        }
    }

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

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;

        if (rank[x] < rank[y]) {
            par[x] = y;
        } else {
            par[y] = x;
            if (rank[x] == rank[y]) rank[x]++;
        }
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }
};

const int MAXE = 1e8;
struct edge { int u, v, cost; };

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

edge es[MAXE];
int V, E;

ll kruskal() {
    sort(es, es+E, comp);
    UnionFind uf;
    uf.init(V);
    ll res = 0;
    for (int i=0; i<E; ++i) {
        edge e = es[i];
        if (!uf.same(e.u, e.v)) {
            uf.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}


int main() {
    cin >> V;
    using P = pair<int, int>;
    vector< P > v(V);
    rep(i, V) {
        P a; int x, y;
        cin >> x >> y;
        a = { x, y };
        v[i] = a;
    }

    rep(i, V) {
        for (int j=i+1; j<V; ++j) {
            edge e = {i, j, min( (int)abs(v[i].first - v[j].first), (int)abs(v[i].second - v[j].second) ) };
            es[E++] = e;
        }
    }

    cout << kruskal() << endl;
}
