#include <bits/stdc++.h>
using namespace std;
struct A {
  int l;
  int r;
  int q;
};
A aa[100005];
int d[10005][10005];
int a[10005], b[10005];
int f[10005];
int finds(int x) { return f[x] == x ? x : f[x] = finds(f[x]); }
int cmp(A a, A b) { return a.q < b.q; }
int main() {
  int n;
  scanf("%d", &n);
  for (register int i = 1; i <= n; ++i) {
    scanf("%d%d", &a[i], &b[i]);
  }
  int ji = 0;
  for (register int i = 1; i <= n; ++i) {
    for (register int j = i + 1; j <= n; ++j) {
      aa[ji].l = i;
      aa[ji].r = j;
      aa[ji++].q = min(abs(a[i] - a[j]), abs(b[i] - b[j]));
    }
  }
  sort(aa, aa + ji, cmp);
  for (register int i = 1; i <= n; ++i) f[i] = i;
  int ans = 0;
  for (int i = 0; i < ji; i++) {
    int x = finds(aa[i].l);
    int y = finds(aa[i].r);
    if (x != y) {
      f[x] = y;
      ans += aa[i].q;
    }
  }
  printf("%d\n", ans);
  return 0;
}
