#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const N = 100 * 1000 + 16;
int const M = 1000 * 1000 * 1000 + 7;
int n, m, k;
int x[N], y[N], idx[N];
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    idx[i] = i;
    cin >> x[i] >> y[i];
  }
  sort(idx, idx + n,
       [](int i, int j) { return tie(x[i], y[i]) < tie(x[j], y[j]); });
  ll ans = 0;
  set<int> Y;
  for (int i = 0, j = 0; i < n;) {
    while (j < n && x[idx[i]] == x[idx[j]]) j++;
    int d = (i == 0) ? 0 : x[idx[i]] - x[idx[i - 1]];
    for (int z = i; z < j; ++z) {
      auto p = Y.lower_bound(y[idx[z]]);
      if (p != Y.end()) d = min(d, *p - y[idx[z]]);
      if (p != Y.end()) {
        p = prev(p);
        if (p != Y.end()) d = min(d, *p - y[idx[z]]);
      }
    }
    while (i < j) Y.insert(y[idx[i++]]);
    ans += d;
  }
  cout << ans << endl;
}
