#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
struct Node {
  int v, id;
  bool operator<(const Node &rhs) const { return v < rhs.v; }
} nx[maxn], ny[maxn];
struct node {
  int x, y;
} n[maxn];
int N;
set<int> xx, yy;
multiset<int> sx, sy;
bool used[maxn];
int lowerbound(Node *f, int l, int r, int v) {
  int lb = l, ub = r, res = -1;
  while (lb <= ub) {
    int mid = (lb + ub) >> 1;
    if (f[mid].v >= v) {
      res = mid;
      ub = mid - 1;
    } else
      lb = mid + 1;
  }
  return res;
}
int main(int argc, char const *argv[]) {
  cin >> N;
  memset(used, false, sizeof(used));
  for (int i = 1; i <= N; i++) {
    scanf("%d%d", &n[i].x, &n[i].y);
    nx[i] = (Node){n[i].x, i};
    ny[i] = (Node){n[i].y, i};
    sx.insert(n[i].x), sy.insert(n[i].y);
  }
  sx.erase(n[1].x), sy.erase(n[1].y);
  xx.insert(n[1].x), yy.insert(n[1].y);
  sort(nx + 1, nx + N + 1);
  sort(ny + 1, ny + N + 1);
  nx[N + 1].v = 2e9 + 5, ny[N + 1].v = 2e9 + 5;
  int cnt = N - 1;
  long long ans = 0;
  while (cnt) {
    int tmp = 2e9;
    pair<int, int> t;
    int base;
    for (int u : xx) {
      for (int i = 0; i < tmp; i++) {
        if (sx.find(u + i) != sx.end()) {
          tmp = i;
          t = make_pair(u + i, 1);
          base = u;
          break;
        }
        if (sx.find(u - i) != sx.end()) {
          tmp = i;
          t = make_pair(u - i, 1);
          base = u;
          break;
        }
      }
    }
    for (int v : yy) {
      for (int i = 0; i < tmp; i++) {
        if (sy.find(v + i) != sy.end()) {
          tmp = i;
          t = make_pair(v + i, 2);
          base = v;
          break;
        }
        if (sy.find(v - i) != sy.end()) {
          tmp = i;
          t = make_pair(v - i, 2);
          base = v;
          break;
        }
      }
    }
    if (t.second == 1) {
      int pos = lowerbound(nx, 1, N + 1, t.first);
      for (int i = pos;; i++)
        if (!used[nx[i].id]) {
          int d = nx[i].id;
          xx.insert(n[d].x);
          yy.insert(n[d].y);
          sx.erase(n[d].x);
          sy.erase(n[d].y);
          used[d] = true;
          break;
        }
      ans += 1ll * abs(t.first - base);
      cnt--;
    } else if (t.second == 2) {
      int pos = lowerbound(ny, 1, N + 1, t.first);
      for (int i = pos;; i++)
        if (!used[ny[i].id]) {
          int d = ny[i].id;
          xx.insert(n[d].x);
          yy.insert(n[d].y);
          sx.erase(n[d].x);
          sy.erase(n[d].y);
          used[d] = true;
          break;
        }
      ans += 1ll * abs(t.first - base);
      cnt--;
    }
  }
  cout << ans << endl;
  return 0;
}
