#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<tuple<int, int, int>> Pos(N);
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    Pos[i] = make_tuple(i, x, y);
  }
  sort(Pos.begin(), Pos.end(),
       [](const tuple<int, int, int> &a, const tuple<int, int, int> &b) {
         return get<1>(a) < get<1>(b);
       });
  vector<vector<pair<int, long long>>> G;
  G = vector<vector<pair<int, long long>>>(N, vector<pair<int, long long>>());
  for (int i = 0; i < N - 1; i++) {
    int n1 = get<0>(Pos[i]), n2 = get<0>(Pos[i + 1]);
    long long dx = abs(get<1>(Pos[i]) - get<1>(Pos[i + 1]));
    long long dy = abs(get<2>(Pos[i]) - get<2>(Pos[i + 1]));
    G[n1].push_back(make_pair(n2, min(dx, dy)));
    G[n2].push_back(make_pair(n1, min(dx, dy)));
  }
  sort(Pos.begin(), Pos.end(),
       [](const tuple<int, int, int> &a, const tuple<int, int, int> &b) {
         return get<2>(a) < get<2>(b);
       });
  for (int i = 0; i < N - 1; i++) {
    int n1 = get<0>(Pos[i]), n2 = get<0>(Pos[i + 1]);
    long long dx = abs(get<1>(Pos[i]) - get<1>(Pos[i + 1]));
    long long dy = abs(get<2>(Pos[i]) - get<2>(Pos[i + 1]));
    G[n1].push_back(make_pair(n2, min(dx, dy)));
    G[n2].push_back(make_pair(n1, min(dx, dy)));
  }
  long long ans = 0;
  vector<bool> used(N, false);
  vector<long long> mincost(N, 1e18);
  mincost[0] = 0;
  while (true) {
    int v = -1;
    for (int i = 0; i < N; i++) {
      if (!used[i] && (v == -1 || mincost[i] < mincost[v])) v = i;
    }
    if (v == -1) break;
    used[v] = true;
    ans += mincost[v];
    for (int i = 0; i < N; i++) {
      long long cost = 1e18;
      for (int j = 0; j < G[v].size(); j++) {
        if (G[v][j].first == i) cost = min(cost, G[v][j].second);
      }
      mincost[i] = min(mincost[i], cost);
    }
  }
  cout << ans << endl;
}
