#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T x) {
  cout << x << endl;
  exit(0);
}
const int maxn = 1e6 + 5;
int n;
long long ans;
set<pair<long long, long long>> todo;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    todo.insert({x, y});
  }
  set<pair<long long, pair<long long, long long>>> dists;
  auto cur = *todo.begin();
  todo.erase(todo.begin());
  for (auto p : todo) {
    long long d = min(abs(cur.first - p.first), abs(cur.second - p.second));
    dists.insert({d, p});
  }
  while (todo.size()) {
    assert(dists.size());
    auto cur = *dists.begin();
    dists.erase(dists.begin());
    long long d = cur.first;
    auto at = cur.second;
    if (!todo.count(at)) continue;
    ans += d;
    todo.erase(at);
    for (auto p : todo) {
      long long d = min(abs(at.first - p.first), abs(at.second - p.second));
      dists.insert({d, p});
    }
  }
  cout << ans << endl;
  return 0;
}
