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

const int MAXV = 100010;
const int INF = 1e8;

int cost[MAXV][MAXV];
int mincost[MAXV];
bool used[MAXV];
int V;

ll prim() {
    for (int i=0; i<V; ++i) {
        mincost[i] = INF;
        used[i] = false;
    }
    mincost[0] = 0;
    ll res = 0;
    while (true) {
        int v = -1;
        for (int u=0;u<V;++u) {
            if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
        }
        if (v == -1) break;
        used[v] = true;
        res += mincost[v];
        for (int u=0; u<V; ++u) {
            mincost[u] = min(mincost[u], cost[v][u]);
        }
    }
    return res;
}

int main() {

    scanf("%d", &V);

    using P = pair<int, int>;
    vector< P > v(V);

    rep(i, V) {
        P a; int x, y;
        scanf("%d %d", &x, &y);
        a = { x, y };
        v[i] = a;
    }

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

    cout << prim() << endl;

}
