#include <bits/stdc++.h>
using namespace std;
int n, m, cnt, cnt0;
int B[100010], C[100010], D[100010], fa[100010];
long long Ans;
struct Point {
  int x, y, id;
} A[100010];
struct edge {
  int u, v, w;
  edge(int u = 0, int v = 0) : u(u), v(v) {}
  friend bool operator<(edge a, edge b) { return a.w < b.w; }
} E[100010 << 3];
inline bool cmpx(Point a, Point b) { return a.x < b.x; }
inline bool cmpy(Point a, Point b) { return a.y < b.y; }
inline bool back(Point a, Point b) { return a.id < b.id; }
inline void reaD(int &x) {
  char c = getchar();
  x = 0;
  int f = 1;
  for (; c > '9' || c < '0'; c = getchar())
    if (c == '-') f = -1;
  for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar())
    ;
  x *= f;
}
inline int query(int x) {
  int Min = 1 << 30, r = -1;
  for (; x <= cnt; x += x & -x)
    if (C[x] < Min) Min = C[x], r = D[x];
  return r;
}
inline void Add(int x, int y, int z) {
  for (; x; x -= x & -x)
    if (y < C[x]) C[x] = y, D[x] = z;
}
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  reaD(n);
  for (int i = 1; i <= n; i++) reaD(A[i].x), reaD(A[i].y), A[i].id = i;
  for (int i = 1; i <= n; i++) B[i] = A[i].y - A[i].x;
  sort(A + 1, A + 1 + n, cmpx);
  sort(B + 1, B + 1 + n);
  cnt = unique(B + 1, B + 1 + n) - B - 1;
  memset(C, 0x7f, sizeof(C));
  memset(D, -1, sizeof(D));
  for (int i = n; i; i--) {
    int x = lower_bound(B + 1, B + 1 + cnt, A[i].y - A[i].x) - B,
        pos = query(x);
    if (pos > 0) E[++cnt0] = edge(A[i].id, pos);
    Add(x, A[i].x + A[i].y, A[i].id);
  }
  memset(C, 0x7f, sizeof(C));
  memset(D, -1, sizeof(D));
  for (int i = 1; i <= n; i++) B[i] = -A[i].x - A[i].y;
  sort(B + 1, B + 1 + n);
  cnt = unique(B + 1, B + 1 + n) - B - 1;
  for (int i = n; i; i--) {
    int x = lower_bound(B + 1, B + 1 + cnt, -A[i].x - A[i].y) - B,
        pos = query(x);
    if (pos > 0) E[++cnt0] = edge(A[i].id, pos);
    Add(x, A[i].x - A[i].y, A[i].id);
  }
  sort(A + 1, A + 1 + n, cmpy);
  memset(C, 0x7f, sizeof(C));
  memset(D, -1, sizeof(D));
  for (int i = 1; i <= n; i++) B[i] = A[i].x - A[i].y;
  sort(B + 1, B + 1 + n);
  cnt = unique(B + 1, B + 1 + n) - B - 1;
  for (int i = n; i; i--) {
    int x = lower_bound(B + 1, B + 1 + cnt, A[i].x - A[i].y) - B,
        pos = query(x);
    if (pos > 0) E[++cnt0] = edge(A[i].id, pos);
    Add(x, A[i].x + A[i].y, A[i].id);
  }
  memset(C, 0x7f, sizeof(C));
  memset(D, -1, sizeof(D));
  for (int i = 1; i <= n; i++) B[i] = A[i].y + A[i].x;
  sort(B + 1, B + 1 + n);
  cnt = unique(B + 1, B + 1 + n) - B - 1;
  for (int i = 1; i <= n; i++) {
    int x = lower_bound(B + 1, B + 1 + cnt, A[i].y + A[i].x) - B,
        pos = query(x);
    if (pos > 0) E[++cnt0] = edge(A[i].id, pos);
    Add(x, A[i].x - A[i].y, A[i].id);
  }
  sort(A + 1, A + 1 + n, back);
  for (int i = 1; i <= cnt0; i++)
    E[i].w =
        min(abs(A[E[i].u].x - A[E[i].v].x), abs(A[E[i].u].y - A[E[i].v].y));
  sort(E + 1, E + 1 + cnt0);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= cnt0; i++) {
    if (find(E[i].u) == find(E[i].v)) continue;
    fa[find(E[i].u)] = find(E[i].v);
    Ans += E[i].w;
  }
  cout << Ans << endl;
  return 0;
}
