#include <bits/stdc++.h>
using namespace std;
long long int cost(pair<long long int, long long int> a,
                   pair<long long int, long long int> b) {
  return min(abs(a.first - b.first), abs(a.second - b.second));
}
int main() {
  long long int n;
  cin >> n;
  long long int x, y;
  vector<pair<long long int, long long int> > d(n);
  for (long long int i = 0; i < n; i++) {
    cin >> x >> y;
    d[i] = pair<long long int, long long int>(x, y);
  }
  vector<long long int> mincost(n, 1000000000000);
  vector<long long int> used(n, 0);
  mincost[0] = 0;
  long long int res = 0;
  while (true) {
    long long int v = -1;
    for (long long int u = 0; u < n; u++) {
      if (used[u] == 0 && (v == -1 || mincost[u] < mincost[v])) v = u;
    }
    if (v == -1) {
      break;
    }
    used[v] = 1;
    res += mincost[v];
    for (long long int u = 0; u < n; u++) {
      mincost[u] = min(mincost[u], cost(d[v], d[u]));
    }
  }
  cout << res << endl;
  return 0;
}
