#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
struct node {
  long long x, y;
  bool operator<(const node& p) const {
    if (x != p.x) return x < p.x;
    return y < p.y;
  }
} a[maxn];
map<long long, long long> mp;
vector<long long> vec[maxn];
int n, d = 0;
long long lr[maxn];
long long minval(int x) {
  int u = x - 1;
  long long tmp = lr[x] - lr[u];
  for (auto v : vec[u]) {
    for (auto k : vec[x]) {
      tmp = min(tmp, abs(v - k));
    }
  }
  return tmp;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y);
  sort(a + 1, a + 1 + n);
  for (long long x, i = 1; i <= n; i++) {
    x = a[i].x;
    if (mp[x]) {
      vec[mp[x]].push_back(a[i].y);
      continue;
    }
    mp[x] = ++d;
    lr[d] = x;
    vec[d].push_back(a[i].y);
  }
  long long ans = 0;
  for (int i = 2; i <= d; i++) ans += minval(i);
  printf("%lld\n", ans);
  return 0;
}
