#include <bits/stdc++.h>
using namespace std;
long long n;
bool used[100000];
typedef pair<long long, long long> pii;
vector<pii> G[100000];
long long prim() {
  priority_queue<pii> que;
  fill(used, used + 100000, false);
  que.push(pii(0, 0));
  long long ret = 0;
  while (!que.empty()) {
    long long v = que.top().second;
    long long cst = (-1) * (que.top().first);
    que.pop();
    if (used[v]) continue;
    used[v] = true;
    ret += cst;
    for (long long i = 0; i < G[v].size(); i++)
      que.push(pii((G[v][i].first) * (-1), (G[v][i].second)));
  }
  return ret;
}
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n;
  long long X[100001], Y[100001];
  for (long long i = 0; i < n; i++) {
    cin >> X[i] >> Y[i];
  }
  for (long long i = 0; i < n - 1; i++) {
    for (long long j = i + 1; j < n; j++) {
      if (i == j) continue;
      G[i].push_back(pii(min(abs(X[i] - X[j]), abs(Y[i] - Y[j])), j));
      G[j].push_back(pii(min(abs(X[i] - X[j]), abs(Y[i] - Y[j])), i));
    }
  }
  cout << prim() << endl;
  return 0;
}
