#include <bits/stdc++.h>
using namespace std;
long long prim(long long n, vector<pair<long long, long long> > locations) {
  long long d[n];
  long long ans = 0;
  fill(d, d + n, 100000000000);
  set<pair<long long, long long> > s;
  for (long long i = 0; i < n; i++) {
    s.insert({d[i], i});
  }
  long long v;
  while (!s.empty()) {
    v = s.begin()->second;
    if (d[v] != 100000000000) {
      ans += d[v];
    }
    d[v] = 0;
    s.erase(s.begin());
    for (long long i = 0; i < n; i++) {
      long long t1 = locations[i].first;
      long long t2 = locations[v].first;
      long long t3 = locations[i].second;
      long long t4 = locations[v].second;
      if (t1 > t2) {
        t1 -= t2;
      } else {
        t1 = (t2 - t1);
      }
      if (t3 > t4) {
        t3 -= t4;
      } else {
        t3 = (t4 - t3);
      }
      long long w = min(t1, t3);
      if (d[i] > w) {
        s.erase({d[i], i});
        d[i] = w;
        s.insert({d[i], i});
      }
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, x, y;
  cin >> n;
  vector<pair<long long, long long> > loc;
  for (long long one = 0; one < n; one++) {
    cin >> x >> y;
    pair<long long, long long> temp = make_pair(x, y);
    loc.push_back(temp);
  }
  cout << prim(n, loc);
}
