#include <bits/stdc++.h>
using namespace std;
int a[100005], b[100005];
int low[100005];
int vis[100005];
int n;
long long ans = 0;
void prim() {
  int pos = 1;
  memset(vis, 0, sizeof(vis));
  for (register int i = 1; i <= n; ++i) {
    if (i != pos) low[i] = min(abs(a[i] - a[1]), abs(b[i] - b[1]));
  }
  vis[pos] = 1;
  for (register int i = 1; i <= n - 1; ++i) {
    int mins = 0x3f3f3f3f;
    for (register int j = 1; j <= n; ++j) {
      if (vis[j] == 0 && low[j] < mins) {
        mins = low[j];
        pos = j;
      }
    }
    ans += mins;
    vis[pos] = 1;
    for (register int j = 1; j <= n; ++j) {
      if (vis[j] == 0)
        low[j] = min(min(abs(a[pos] - a[j]), abs(b[pos] - b[j])), low[j]);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (register int i = 1; i <= n; ++i) {
    scanf("%d%d", &a[i], &b[i]);
  }
  prim();
  printf("%d\n", ans);
  return 0;
}
