#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
struct point {
  long long x, y, id;
} p[N];
priority_queue<pair<long long, pair<long long, long long>>,
               vector<pair<long long, pair<long long, long long>>>,
               greater<pair<long long, pair<long long, long long>>>>
    pq;
long long n, par[N], sz[N];
bool cmp1(const point &a, const point &b) { return a.x < b.x; }
bool cmp2(const point &a, const point &b) { return a.y < b.y; }
long long getp(long long u) { return u == par[u] ? u : par[u] = getp(par[u]); }
bool joint(long long u, long long v) {
  u = getp(u), v = getp(v);
  if (u == v) return false;
  if (sz[u] < sz[v]) swap(u, v);
  sz[u] += sz[v];
  par[v] = u;
  return true;
}
long long dist(point x, point y) { return min(abs(x.x - y.x), abs(x.y - y.y)); }
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) par[i] = i, sz[i] = 1;
  for (long long i = 0; i < n; i++) cin >> p[i].x >> p[i].y, p[i].id = i;
  sort(p, p + n, cmp1);
  for (long long i = 1; i < n; i++) {
    pq.push(pair<long long, pair<long long, long long>>(
        dist(p[i], p[i - 1]),
        pair<long long, long long>(p[i].id, p[i - 1].id)));
  }
  long long ans = 0;
  while (!pq.empty()) {
    pair<long long, pair<long long, long long>> foo = pq.top();
    pq.pop();
    long long u = foo.second.first, v = foo.second.second, w = foo.first;
    if (joint(u, v)) {
      ans += w;
    }
  }
  cout << ans;
  return 0;
}
