#include <bits/stdc++.h>
using namespace std;
class point {
 public:
  int x;
  int y;
  bool used;
  int nfast;
  point() : used(false), nfast((1 << 31) - 1) {}
};
int count(vector<point> &, int, int);
int main() {
  int n;
  cin >> n;
  vector<point> map(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> map[i].x >> map[i].y;
  }
  map[1].used = true;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      que;
  for (int i = 2; i <= n; ++i) {
    int num = count(map, 1, i);
    map[i].nfast = num;
    que.push(pair<int, int>(num, i));
  }
  long long ans = 0;
  while (!que.empty()) {
    pair<int, int> du = que.top();
    que.pop();
    if (map[du.second].used) continue;
    map[du.second].used = true;
    ans += du.first;
    for (int i = 1; i <= n; ++i) {
      if (map[i].used) continue;
      int num = count(map, du.second, i);
      if (map[i].nfast > num) {
        que.push(pair<int, int>(num, i));
        map[i].nfast = num;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
int count(vector<point> &line, int a, int b) {
  return min(abs(line[a].x - line[b].x), abs(line[a].y - line[b].y));
}
