#include <bits/stdc++.h>
using namespace std;
long long par[100010];
long long rk[100010];
struct edge {
  long long u, v, cost;
};
edge es[100010 * 2];
long long n;
void init(long long n) {
  for (long long i = 0; i < n; i++) {
    par[i] = i;
    rk[i] = 0;
  }
}
long long find(long long x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(long long x, long long y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  if (rk[x] < rk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rk[x] == rk[y]) rk[x]++;
  }
}
bool same(long long x, long long y) { return find(x) == find(y); }
bool comp(const edge& e1, const edge& e2) { return e1.cost < e2.cost; }
long long kruskal() {
  long long m = 2 * n - 2;
  sort(es, es + m, comp);
  init(m);
  long long res = 0;
  for (long long i = 0; i < m; i++) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}
signed main() {
  cin >> n;
  long long x[n], y[n];
  for (long long i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  pair<long long, long long> xres[n], yres[n];
  for (long long i = 0; i < n; i++) {
    xres[i] = pair<long long, long long>(x[i], i);
    yres[i] = pair<long long, long long>(y[i], i);
  }
  sort(xres, xres + n);
  sort(yres, yres + n);
  for (long long i = 0; i < n - 1; i++) {
    edge tmp;
    tmp.u = xres[i].second;
    tmp.v = xres[i + 1].second;
    tmp.cost = abs(xres[i + 1].first - xres[i].first);
    es[i] = tmp;
  }
  for (long long i = n - 1; i < 2 * n - 2; i++) {
    edge tmp;
    tmp.u = yres[i - n + 1].second;
    tmp.v = yres[i - n + 2].second;
    tmp.cost = abs(yres[i - n + 1].first - yres[i - n + 2].first);
    es[i] = tmp;
  }
  cout << kruskal() << endl;
  return 0;
}
