#include <bits/stdc++.h>
using namespace std;
long k = 10e9 + 7;
struct pos {
  int x;
  int y;
};
int cost(pos a, pos b) { return min(abs(a.x - b.x), abs(a.y - b.y)); }
int cost(vector<pos> g, pos a) {
  int mc = cost(g[0], a);
  for (pos p : g) {
    int tc = cost(p, a);
    if (tc < mc) {
      mc = tc;
    }
  }
  return mc;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long n;
  cin >> n;
  vector<pos> cs(n);
  for (int i = 0; i < n; i++) {
    cin >> cs[i].x >> cs[i].y;
  }
  vector<pos> g;
  g.push_back(cs[0]);
  cs.erase(cs.begin());
  int total_cost = 0;
  while (cs.size() > 0) {
    int mc = cost(g, cs[0]);
    int mi = 0;
    for (int i = 0; i < cs.size(); i++) {
      int tc = cost(g, cs[i]);
      if (tc < mc) {
        mi = i;
        mc = tc;
      }
    }
    total_cost += mc;
    g.push_back(cs[mi]);
    cs.erase(cs.begin() + mi);
  }
  cout << total_cost << endl;
  return 0;
}
