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