#include <bits/stdc++.h>
using namespace std;
struct UF {
  vector<int> par, rank;
  UF(int n) {
    for (int i = 0; i < n; i++) {
      par.emplace_back(i);
    }
    rank.assign(n, 0);
  }
  int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (rank[x] < rank[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
};
int64_t dist(pair<int64_t, int64_t> a, pair<int64_t, int64_t> b) {
  return min(abs(a.first - b.first), abs(a.second - b.second));
}
int main() {
  int n;
  cin >> n;
  pair<int64_t, int64_t> p[n];
  for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second;
  int64_t ans = 0;
  UF uf(n);
  for (int i = 0; i < n; i++) {
    int po = -1;
    pair<int64_t, int64_t> puni = {1ll << 60, 1ll << 60};
    for (int j = 0; j < n; j++)
      if (i != j && !uf.same(i, j) && dist(p[i], p[j]) < dist(p[i], puni)) {
        po = j;
        puni = p[j];
      }
    if (po != -1) {
      ans += dist(p[i], puni);
      uf.unite(i, po);
    }
  }
  cout << ans << endl;
}
