#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
pair<int, int> s[N];
int mabs(int x) { return x < 0 ? -x : x; }
int id1[N], id2[N];
bool cmp1(int i, int j) {
  return mabs(s[i].first - s[0].first) < mabs(s[j].first - s[0].first);
}
bool cmp2(int i, int j) {
  return mabs(s[i].second - s[0].second) < mabs(s[j].second - s[0].second);
}
bool b[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < (n); ++i) cin >> s[i].first >> s[i].second;
  for (int i = 0; i < (n); ++i) id1[i] = id2[i] = i;
  sort(id1 + 1, id1 + n, cmp1);
  sort(id2 + 1, id2 + n, cmp2);
  b[0] = 1;
  long long ans = 0;
  set<int> se1, se2;
  se1.insert(s[0].first);
  se2.insert(s[0].second);
  int p1 = 1, p2 = 1;
  while (p1 < n && p2 < n) {
    while (p1 < n && b[id1[p1]]) p1++;
    while (p2 < n && b[id2[p2]]) p2++;
    if (p1 == n || p2 == n) break;
    int x = INF, y = INF;
    auto it1 = se1.lower_bound(s[id1[p1]].first);
    if (it1 != se1.end()) x = min(x, mabs(*it1 - s[id1[p1]].first));
    if (it1 != se1.begin()) x = min(x, mabs(*(--it1) - s[id1[p1]].first));
    auto it2 = se2.lower_bound(s[id2[p2]].second);
    if (it2 != se2.end()) y = min(y, mabs(*it2 - s[id2[p2]].second));
    if (it2 != se2.begin()) y = min(y, mabs(*(--it2) - s[id2[p2]].second));
    if (x < y) {
      ans += x, b[id1[p1]] = 1;
      se1.insert(s[id1[p1]].first);
      se2.insert(s[id1[p1]].second);
      p1++;
    } else {
      ans += y, b[id2[p2]] = 1;
      se1.insert(s[id2[p2]].first);
      se2.insert(s[id2[p2]].second);
      p2++;
    }
  }
  cout << ans << endl;
  return 0;
}
