#include <bits/stdc++.h>
using namespace std;
void inline minize(long long& a, long long b) { a = (a > b ? b : a); };
void inline maxize(long long& a, long long b) { a = (a < b ? b : a); };
double const eps = 1e-6;
long long const Base = 1e9 + 7, oo = 1e17;
long long mark[1000000], fac[100005];
pair<long long, long long> arr[100005];
class cmp_fi {
 public:
  bool operator()(pair<long long, long long> a, pair<long long, long long> b) {
    return a.first > b.first;
  }
};
long long Solves() {
  long long n, m, k, cnt = 0, ans = 0, x, x1, x2, y1, y2, p, v;
  map<long long, long long> ne_x, ne_y, po_x, po_y;
  ne_x[-oo] = -1;
  ne_x[oo] = -1;
  ne_y[-oo] = -1;
  ne_y[oo] = -1;
  po_x[-oo] = -1;
  po_x[oo] = -1;
  po_y[-oo] = -1;
  po_y[oo] = -1;
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >, cmp_fi>
      pq;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> arr[i].first >> arr[i].second;
    if (!i) continue;
    po_x[arr[i].first] = i;
    ne_x[-arr[i].first] = i;
    po_y[arr[i].second] = i;
    ne_y[-arr[i].second] = i;
  }
  pair<long long, long long> mypair;
  v = oo;
  mypair = *po_x.upper_bound(arr[0].first);
  pq.push(make_pair(abs(arr[0].first - mypair.first), mypair.second));
  mypair = *po_y.upper_bound(arr[0].second);
  pq.push(make_pair(abs(arr[0].second - mypair.first), mypair.second));
  mypair = *ne_x.upper_bound(-arr[0].first);
  pq.push(make_pair(abs(arr[0].first + mypair.first), mypair.second));
  mypair = *ne_y.upper_bound(-arr[0].second);
  pq.push(make_pair(abs(arr[0].second + mypair.first), mypair.second));
  for (long long i = 1; i < n; i++) {
    while (mark[pq.top().second]) pq.pop();
    p = pq.top().second;
    v = pq.top().first;
    pq.pop();
    ans += v;
    mark[p] = 1;
    po_x.erase(arr[p].first);
    ne_x.erase(-arr[p].first);
    po_y.erase(arr[p].second);
    ne_y.erase(-arr[p].second);
    mypair = *po_x.upper_bound(arr[p].first);
    pq.push(make_pair(abs(arr[p].first - mypair.first), mypair.second));
    mypair = *po_y.upper_bound(arr[p].second);
    pq.push(make_pair(abs(arr[p].second - mypair.first), mypair.second));
    mypair = *ne_x.upper_bound(-arr[p].first);
    pq.push(make_pair(abs(arr[p].first + mypair.first), mypair.second));
    mypair = *ne_y.upper_bound(-arr[p].second);
    pq.push(make_pair(abs(arr[p].second + mypair.first), mypair.second));
  }
  cout << ans << " ";
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  Solves();
}
