#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B>
inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1ll << 30;
const long long MOD = 1000000007;
const double EPS = 1e-10;
int N;
int x[112345], y[112345];
bool done[112345];
int main() {
  cin >> N;
  for (int i = (0); i < (N); i++) scanf("%d %d", x + i, y + i);
  set<pair<int, int> > ss;
  for (int i = (0); i < (N); i++) ss.insert(pair<int, int>(x[i], y[i]));
  vector<pair<int, int> > v;
  for (set<pair<int, int> >::iterator it = ss.begin(); it != ss.end(); ++it)
    v.push_back(*it);
  vector<pair<int, int> > x, y;
  for (int i = (0); i < (v.size()); i++) {
    x.push_back(pair<int, int>(v[i].first, i));
    y.push_back(pair<int, int>(v[i].second, i));
  }
  sort((x).begin(), (x).end());
  sort((y).begin(), (y).end());
  priority_queue<pair<int, pair<long long, long long> >,
                 vector<pair<int, pair<long long, long long> > >,
                 greater<pair<int, pair<long long, long long> > > >
      pq;
  for (int i = (0); i < (x.size() - 1); i++)
    pq.push(pair<int, pair<long long, long long> >(
        x[i + 1].first - x[i].first,
        pair<int, int>(x[i + 1].second, x[i].second)));
  for (int i = (0); i < (y.size() - 1); i++)
    pq.push(pair<int, pair<long long, long long> >(
        y[i + 1].first - y[i].first,
        pair<int, int>(y[i + 1].second, y[i].second)));
  long long ans = 0;
  while (!pq.empty()) {
    int p = pq.top().second.first;
    int q = pq.top().second.second;
    pq.pop();
    if (done[p] && done[q]) continue;
    ans += min(abs(v[p].first - v[q].first), abs(v[p].second - v[q].second));
    done[p] = done[q] = true;
  }
  cout << ans << endl;
  return 0;
}
