#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
const int INF = 0x7ffffff0;
int N;
int x[MAX_N];
int y[MAX_N];
long long cost(int i, int j) { return min(abs(x[i] - x[j]), abs(y[i] - y[j])); }
typedef pair<long long, int> pii;
long long prim() {
  priority_queue<pii, vector<pii>, greater<pii> > que;
  vector<bool> used(N, false);
  que.push(pii(0, 0));
  long long ret = 0;
  while (!que.empty()) {
    long long cst = que.top().first;
    int v = que.top().second;
    que.pop();
    if (used[v]) continue;
    used[v] = true;
    ret += cst;
    for (int i = 0; i < N; i++) que.push(pii(cost(v, i), i));
  }
  return ret;
}
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &x[i], &y[i]);
  }
  printf("%lld\n", prim());
}
