#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int N;
ll prim(vector<vector<ll> > &G) {
  ll ans = 0ll;
  vector<bool> used(N, false);
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      Q;
  Q.push(make_pair(0, 0));
  while (!Q.empty()) {
    int u;
    ll cost_u;
    tie(cost_u, u) = Q.top();
    Q.pop();
    if (used[u]) continue;
    used[u] = true;
    ans += cost_u;
    for (int j = 0; j < N; j++) {
      Q.push(make_pair(G[u][j], j));
    }
  }
  return ans;
}
int main() {
  cin >> N;
  vector<pair<int, int> > towns(N);
  vector<vector<ll> > G(N, vector<ll>(N));
  int x, y;
  for (int i = 0; i < N; i++) {
    scanf("%d%d", &x, &y);
    towns[i] = make_pair(x, y);
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      ll cost = min(abs(towns[i].first - towns[j].first),
                    abs(towns[i].second - towns[j].second));
      G[i][j] = cost;
    }
  }
  cout << prim(G) << endl;
  return 0;
}
