#include <bits/stdc++.h>
using namespace std;
namespace UF {
int par[100010], rnk[100010];
void rem(int i) { par[i] = i, rnk[i] = 0; }
void init(int n) {
  for (int i = 1; i <= n; i++) par[i] = i, rnk[i] = 0;
}
int find(int x) {
  if (x == par[x]) return x;
  return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return;
  if (rnk[x] < rnk[y])
    par[x] = y;
  else {
    par[y] = x;
    if (rnk[x] == rnk[y]) rnk[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
};  // namespace UF
using UF::init;
using UF::same;
using UF::unite;
struct Edge {
  int u, v, c;
};
struct coo {
  int u, v, id;
};
int n;
int a[100010];
coo co[100010];
Edge E[100010];
int m;
int kruskal() {
  sort(E + 1, E + m + 1,
       [](const Edge& x, const Edge& y) { return x.c < y.c; });
  init(n);
  int res = 0;
  for (int i = 1; i <= m; i++) {
    Edge cur = E[i];
    if (!same(cur.u, cur.v)) {
      unite(cur.u, cur.v);
      res += cur.c;
    }
  }
  return res;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> co[i].u >> co[i].v;
    co[i].id = i;
  }
  sort(co + 1, co + n + 1,
       [](const coo& x, const coo& y) { return x.u < y.u; });
  for (int i = 2; i <= n; i++) {
    int d = abs(co[i - 1].u - co[i].u);
    int pri = co[i - 1].id, nxi = co[i].id;
    E[++m] = (Edge){nxi, pri, d};
    E[++m] = (Edge){pri, nxi, d};
  }
  sort(co + 1, co + n + 1,
       [](const coo& x, const coo& y) { return x.v < y.v; });
  for (int i = 2; i <= n; i++) {
    int d = abs(co[i - 1].v - co[i].v);
    int pri = co[i - 1].id, nxi = co[i].id;
    E[++m] = (Edge){nxi, pri, d};
    E[++m] = (Edge){pri, nxi, d};
  }
  cout << kruskal() << endl;
  return 0;
}
