#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
const long long INF = 999999999;
long long cost(pair<long long, long long> p1, pair<long long, long long> p2) {
  return min(abs(p1.first - p2.first), abs(p1.second - p2.second));
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N;
  cin >> N;
  vector<pair<long long, long long>> city;
  for (long long i = 0; i < (N); i++) {
    long long x, y;
    cin >> x >> y;
    city.push_back(make_pair(x, y));
  }
  vector<long long> mincost(N, INF);
  vector<bool> used(N, false);
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      Q;
  long long res = 0;
  Q.emplace(0, 0);
  while (!Q.empty()) {
    pair<long long, long long> v = Q.top();
    Q.pop();
    if (used[v.second]) continue;
    used[v.second] = true;
    res += v.first;
    for (long long i = 0; i < (N); i++) {
      if (i != v.second) Q.emplace(cost(city[v.second], city[i]), i);
    }
  }
  cout << res << "\n";
  return 0;
}
