#include <bits/stdc++.h>
using namespace std;
pair<int, int> x[1 << 17], y[1 << 17];
int i, n, p[1 << 17], s;
pair<int, pair<int, int> > G[2 << 17];
int f(int a) { return p[a] ^ a ? p[a] = f(p[a]) : a; }
int main() {
  for (cin >> n; i++ < n; x[i].second = y[i].second = p[i] = i)
    cin >> x[i].first >> y[i].first;
  sort(x + 1, x + n + 1), sort(y + 1, y + n + 1);
  for (i = 0; ++i < n;) {
    G[i * 2] = make_pair(abs(x[i].first - x[i + 1].first),
                         make_pair(x[i].second, x[i + 1].second));
    G[i * 2 + 1] = make_pair(abs(y[i].first - y[i + 1].first),
                             make_pair(y[i].second, y[i + 1].second));
  }
  sort(G, G + n * 2 - 2);
  for (i = 0; i < 2 * n - 2; i++) {
    int x = G[i].second.first, y = G[i].second.second;
    if (f(x) ^ f(y)) {
      s += G[i].first;
      p[f(x)] = f(y);
    }
  }
  cout << s << endl;
}
