#include <bits/stdc++.h>
inline int getint() {
  int x = 0, p = 1;
  char c;
  c = getchar();
  while (c <= 32) c = getchar();
  if (c == 45) p = -p, c = getchar();
  while (c > 32) x = x * 10 + c - 48, c = getchar();
  return x * p;
}
using namespace std;
const int maxn = 1e5 + 5;
int n, par[maxn];
long long res;
vector<pair<int, int> > v;
set<pair<int, pair<int, int> > > S;
vector<pair<int, pair<int, int> > > E;
void finit() {
  for (int(i) = 0; (i) < (n); (i)++) par[i] = i;
}
int fF(int x) {
  if (par[x] == x) return x;
  return par[x] = fF(par[x]);
}
bool fU(int x, int y) {
  x = fF(x);
  y = fF(y);
  if (x == y) return false;
  par[x] = y;
  return true;
}
inline int getlen(pair<int, int> x, pair<int, int> y) {
  int ans = min(abs(x.first - y.first), abs(x.second - y.second));
  return ans;
}
int main() {
  n = getint();
  for (int(i) = 0; (i) < (n); (i)++) {
    int x = getint(), y = getint();
    v.push_back(make_pair(x, y));
    S.insert(make_pair(y, make_pair(x, i)));
  }
  sort(v.begin(), v.end());
  for (int(i) = 0; (i) < (v.size() - 1); (i)++) {
    int len = getlen(v[i + 1], v[i]);
    E.push_back(make_pair(len, make_pair(i, i + 1)));
  }
  set<pair<int, pair<int, int> > >::iterator it;
  for (it = S.begin(); it != S.end(); it++) {
    it++;
    if (it == S.end()) break;
    it--;
    set<pair<int, pair<int, int> > >::iterator it2 = it;
    it2++;
    E.push_back(make_pair(abs(it2->first - it->first),
                          make_pair(it->second.second, it2->second.second)));
  }
  sort(E.begin(), E.end());
  finit();
  for (int(i) = 0; (i) < (E.size()); (i)++) {
    long long len = 1ll * E[i].first;
    int x = E[i].second.first, y = E[i].second.second;
    if (fU(x, y)) {
      res += len;
    }
  }
  printf("%lld\n", res);
  return 0;
}
