#include <bits/stdc++.h>
using namespace std;
int n, d[100000];
pair<int, int> p[100000];
set<int> y, c, g;
set<pair<int, int> > s;
long long yen = 0;
int dist(int a, int b) {
  return min(abs(p[a].first - p[b].first), abs(p[a].second - p[b].second));
}
void f(int z) {
  g.insert(z);
  c.erase(z);
  if (z - 1 >= 0 && z - 1 < n) {
    if (g.find(z - 1) == g.end()) {
      c.insert(z - 1);
      d[z - 1] = min(d[z - 1], dist(z - 1, z));
    }
  }
  if (z + 1 >= 0 && z + 1 < n) {
    if (g.find(z + 1) == g.end()) {
      c.insert(z + 1);
      d[z + 1] = min(d[z + 1], dist(z + 1, z));
    }
  }
  auto itrZ = s.find(pair<int, int>(p[z].second, z));
  if (itrZ != s.end()) {
    auto itrP = itrZ;
    itrP++;
    if (itrP != s.end()) {
      int zp = (*itrP).second;
      if (g.find(zp) == g.end()) {
        c.insert(zp);
        d[zp] = min(d[zp], dist(zp, z));
      }
    }
    if (itrZ != s.begin()) {
      itrZ--;
      int zp = (*itrZ).second;
      if (g.find(zp) == g.end()) {
        c.insert(zp);
        d[zp] = min(d[zp], dist(zp, z));
      }
    }
  }
}
int main(void) {
  cin >> n;
  fill(d, d + n, 1e9 + 7);
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
  }
  sort(p, p + n);
  for (int i = 0; i < n; i++) {
    s.insert(pair<int, int>(p[i].second, i));
  }
  f(0);
  while (!c.empty()) {
    int ind = -1, cost = 1e9 + 7;
    for (auto i = c.begin(); i != c.end(); i++) {
      int j = (*i);
      if (ind == -1) {
        ind = j;
        cost = d[j];
      } else if (d[j] < cost) {
        ind = j;
        cost = d[j];
      }
    }
    f(ind);
    yen += (long long)cost;
  }
  cout << yen << endl;
}
