#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  scanf("%lld", &N);
  vector<long long> x(N - 1 + 1);
  vector<long long> y(N - 1 + 1);
  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> > > >
      quex, quey;
  for (long long i = 0; i <= N - 1; i++) {
    scanf("%lld", &x[i]);
    scanf("%lld", &y[i]);
  }
  vector<pair<long long, long long> > px(N - 1 + 1);
  vector<pair<long long, long long> > py(N - 1 + 1);
  for (long long i = 0; i <= N - 1; i++) {
    px[i] = pair<long long, long long>(x[i], i);
    py[i] = pair<long long, long long>(y[i], i);
  }
  sort(px.begin(), px.end());
  sort(py.begin(), py.end());
  for (long long i = 0; i < N - 1; i++) {
    quex.push(pair<long long, pair<long long, long long> >(
        abs(px[i].first - px[i + 1].first),
        pair<long long, long long>(px[i].second, px[i + 1].second)));
    quey.push(pair<long long, pair<long long, long long> >(
        abs(py[i].first - py[i + 1].first),
        pair<long long, long long>(py[i].second, py[i + 1].second)));
  }
  bool used[N];
  for (long long i = 0; i <= N - 1; i++) {
    used[i] = false;
  }
  long long res = 0;
  while (!quex.empty() || !quey.empty()) {
    if (quey.empty()) {
      if (!used[quex.top().second.first] || !used[quex.top().second.second])
        res += quex.top().first;
      used[quex.top().second.first] = true;
      used[quex.top().second.second] = true;
      quex.pop();
    } else if (quex.empty()) {
      if (!used[quey.top().second.first] || !used[quey.top().second.second])
        res += quey.top().first;
      used[quey.top().second.first] = true;
      used[quey.top().second.second] = true;
      quey.pop();
    } else {
      if (quex.top().first < quey.top().first) {
        if (!used[quex.top().second.first] || !used[quex.top().second.second])
          res += quex.top().first;
        used[quex.top().second.first] = true;
        used[quex.top().second.second] = true;
        quex.pop();
      } else {
        if (!used[quey.top().second.first] || !used[quey.top().second.second])
          res += quey.top().first;
        used[quey.top().second.first] = true;
        used[quey.top().second.second] = true;
        quey.pop();
      }
    }
  }
  printf("%lld\n", res);
  return 0;
}
