#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<pair<int, int> > v, xs, ys;
  int x, y, i, n, ub, ub1;
  int ans[100001];
  long long int sum(0);
  memset(ans, 1000000007, sizeof(ans));
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
    xs.push_back(make_pair(x, i));
    ys.push_back(make_pair(y, i));
  }
  sort(xs.begin(), xs.end());
  sort(ys.begin(), ys.end());
  for (i = 0; i < n; i++) {
    if (i == 0) {
      xs[i].first = xs[i + 1].first - xs[i].first;
      ys[i].first = ys[i + 1].first - ys[i].first;
    } else if (i == n - 1) {
      xs[i].first = xs[i].first - xs[i - 1].first;
      ys[i].first = ys[i].first - ys[i - 1].first;
    } else {
      xs[i].first =
          min(xs[i + 1].first - xs[i].first, xs[i].first - xs[i - 1].first);
      ys[i].first =
          min(ys[i + 1].first - ys[i].first, ys[i].first - ys[i - 1].first);
    }
  }
  for (i = 0; i < n; i++) {
    ans[xs[i].second] = xs[i].first;
  }
  for (i = 0; i < n; i++) {
    ans[ys[i].second] = min(ans[ys[i].second], ys[i].first);
  }
  for (i = 0; i < n - 1; i++) {
    sum += ans[i];
  }
  cout << sum;
  return 0;
}
