#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int MOD = 1e9 + 7;
long long dist[N];
bool vis[N];
int head[N], tot = 0, fa[N];
struct node {
  int from, to;
  long long w;
  int next;
} edge[N * 4];
bool operator<(node a, node b) { return a.w < b.w; }
void add(int u, int v, long long w) {
  edge[tot].from = u;
  edge[tot].to = v;
  edge[tot].w = w;
  edge[tot].next = head[u];
  head[u] = tot++;
}
int n;
int get(int x) {
  if (x == fa[x])
    return x;
  else
    return fa[x] = get(fa[x]);
}
int x[N], y[N];
int main() {
  memset(head, -1, sizeof(head));
  tot = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &y[i]);
  }
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      add(i, j, min(abs(x[i] - x[j]), abs(y[i] - y[j])));
    }
  }
  sort(edge, edge + tot);
  int cnt = 0;
  long long ans = 0;
  for (int i = 0; i < tot; i++) {
    int fu = get(edge[i].from);
    int fv = get(edge[i].to);
    if (fu != fv) {
      fa[fu] = fv;
      cnt++;
      ans += edge[i].w;
      if (cnt == n - 1) break;
    }
  }
  cout << ans << endl;
  return 0;
}
