#include <bits/stdc++.h>
using namespace std;
using namespace std;
int n, x[100005], y[100005];
long long d[100005];
vector<int> G[100005];
set<pair<int, int> > s;
pair<int, int> u;
bool checked[100005];
long long dist(int i, int j) { return min(abs(x[i] - x[j]), abs(y[i] - y[j])); }
int main() {
  cin >> n;
  for (int i = (1); i <= n; i++) {
    cin >> x[i] >> y[i];
  }
  for (int i = (1); i <= n; i++) d[i] = 1000000000;
  d[1] = 0;
  for (int i = (1); i <= n; i++) s.insert(make_pair(d[i], i));
  for (int i = (1); i <= n; i++) checked[i] = false;
  while (!s.empty()) {
    u = *s.begin();
    s.erase(s.begin());
    checked[u.second] = true;
    for (int v = (1); v <= n; v++) {
      if (!checked[v] && d[v] > dist(u.second, v)) {
        s.erase(make_pair(d[v], v));
        d[v] = dist(u.second, v);
        s.insert(make_pair(d[v], v));
      }
    }
  }
  long long res = 0;
  for (int i = (1); i <= n; i++) res += d[i];
  cout << res << endl;
}
