#include <bits/stdc++.h>
using namespace std;
struct edge {
  int from, to;
  long long cost;
};
long long mincost[100005];
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    que;
bool used[100005];
int V;
long long prim(vector<vector<long long> > cost) {
  fill(mincost, mincost + V, 1410065408);
  fill(used, used + V, false);
  que.push(make_pair(0, 0));
  int res = 0;
  while (true) {
    pair<long long, long long> v;
    while (!que.empty()) {
      if (!used[que.top().second]) {
        v = que.top();
        break;
      }
      que.pop();
    }
    if (que.empty()) break;
    used[v.second] = true;
    res += v.first;
    for (int i = 0; i < V; i++) {
      mincost[i] = min(mincost[i], cost[v.second][i]);
      que.push(make_pair(mincost[i], i));
    }
  }
  return res;
}
signed main() {
  cin >> V;
  vector<long long> x(V), y(V);
  vector<vector<long long> > cost(V, vector<long long>(V));
  for (int i = 0; i < V; i++) {
    cin >> x[i] >> y[i];
    for (int j = 0; j < i; j++) {
      cost[i][j] = cost[j][i] = min(abs(x[i] - x[j]), abs(y[i] - y[j]));
    }
  }
  printf("%lld\n", prim(cost));
  return 0;
}
