#include <bits/stdc++.h>
using namespace std;
vector<int> x, y;
deque<pair<int, pair<int, int>>> p;
set<int> contain;
vector<int> uf;
int find(int x) {
  if (uf[x] == x) {
    return x;
  } else {
    return uf[x] = find(uf[x]);
  }
}
void unio(int x, int y) { uf[find(x)] = uf[find(y)]; }
map<pair<int, int>, int> costs;
pair<int, int> mp(int x, int y) {
  if (x < y) {
    return make_pair(x, y);
  } else {
    return make_pair(y, x);
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int xx, yy;
    cin >> xx >> yy;
    x.push_back(xx);
    y.push_back(yy);
  }
  for (int i = 0; i < n; i++) {
    uf.push_back(i);
  }
  int nn = n;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int cost = min(abs(x[i] - x[j]), abs(y[i] - y[j]));
      if (cost == 0) {
        unio(i, j);
        nn--;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int cost = min(abs(x[i] - x[j]), abs(y[i] - y[j]));
      int fi = find(i);
      int fj = find(j);
      auto pa = mp(fi, fj);
      if (fi != fj) {
        if (costs.count(pa) && costs[pa] < cost) {
          continue;
        }
        costs[pa] = cost;
        p.push_back(make_pair(cost, make_pair(i, j)));
      }
    }
  }
  sort(p.begin(), p.end());
  int sum = 0;
  for (int i = 1; i < nn;) {
    auto mm = p.front();
    if (find(mm.second.first) != find(mm.second.second)) {
      unio(mm.second.first, mm.second.second);
      sum += mm.first;
      i++;
    }
    p.pop_front();
  }
  cout << sum << endl;
  return 0;
}
