#include <bits/stdc++.h>
using namespace std;
bool mylesser(pair<pair<long long, long long>, int> a,
              pair<pair<long long, long long>, int> b) {
  return a.first.second < b.first.second;
}
long long prim(int V, vector<vector<long long> > cost) {
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      que;
  vector<long long> mincost(V, 3000000000);
  vector<bool> used(V, false);
  que.push(make_pair(0, 0));
  long long res = 0;
  while (true) {
    pair<long long, int> v;
    while (!que.empty()) {
      if (!used[que.top().second]) {
        v = que.top();
        break;
      }
      que.pop();
    }
    if (que.empty()) break;
    used[v.second] = true;
    res += v.first;
    for (int i = 0; i < V; i++) {
      mincost[i] = min(mincost[i], cost[v.second][i]);
      que.push(make_pair(mincost[i], i));
    }
  }
  return res;
}
int main() {
  int V;
  scanf("%d", &V);
  vector<pair<pair<long long, long long>, int> > p(V);
  vector<vector<long long> > cost(V, vector<long long>(V, 3000000000));
  for (int i = 0; i < V; i++) {
    scanf("%lld %lld", &p[i].first.first, &p[i].first.second);
    p[i].second = i;
  }
  sort(p.begin(), p.end());
  for (int i = 0; i < V - 1; i++) {
    pair<long long, long long> vvi = p[i].first, wwi = p[i + 1].first;
    cost[p[i].second][p[i + 1].second] = cost[p[i + 1].second][p[i].second] =
        min(abs(vvi.first - wwi.first), abs(vvi.second - wwi.second));
  }
  sort(p.begin(), p.end(), mylesser);
  for (int i = 0; i < V - 1; i++) {
    pair<long long, long long> vvi = p[i].first, wwi = p[i + 1].first;
    cost[p[i].second][p[i + 1].second] = cost[p[i + 1].second][p[i].second] =
        min(abs(vvi.first - wwi.first), abs(vvi.second - wwi.second));
  }
  printf("%lld\n", prim(V, cost));
  return 0;
}
