#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  vector<pair<int, int> > g;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    g.push_back(pair<int, int>(x, y));
  }
  int ret = 0;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  vector<int> used(n + 1, 0);
  q.push(pair<int, int>(0, 1));
  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    if (used[p.second]++) continue;
    ret += p.first;
    for (int i = 0; i < n; i++) {
      if (p.second == i) continue;
      q.push(pair<int, int>(min(abs(g[p.second].first - g[i].first),
                                abs(g[p.second].second - g[i].second)),
                            i));
    }
  }
  cout << ret << endl;
}
