#include <bits/stdc++.h>
using namespace std;
const long long maxn = 10050, inf = LONG_LONG_MAX;
long long x[maxn], y[maxn], Map[maxn][maxn], vis[maxn], ans, n, d[maxn];
struct node {
  long long v, len;
  friend bool operator<(node a, node b) { return a.len > b.len; }
};
long long prim() {
  priority_queue<node> q;
  node t;
  t.v = 1;
  t.len = 0;
  q.push(t);
  ans = 0;
  fill(vis, vis + maxn, 0);
  fill(d, d + maxn, inf);
  while (q.size()) {
    t = q.top();
    q.pop();
    if (vis[t.v]) continue;
    vis[t.v] = 1;
    ans += t.len;
    for (int i = 1; i <= n; i++) {
      if (!vis[i] && Map[t.v][i] < d[i]) {
        node next;
        next.v = i;
        next.len = Map[t.v][i];
        d[i] = Map[t.v][i];
        q.push(next);
      }
    }
  }
  return ans;
}
int main() {
  cin.sync_with_stdio(0);
  while (cin >> n) {
    for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        Map[i][j] = min(abs(x[i] - x[j]), abs(y[i] - y[j]));
    cout << prim() << endl;
  }
  return 0;
}
