#include <bits/stdc++.h>
using namespace std;
ofstream outfile;
ifstream infile;
vector<pair<int, int>> leader;
int findleader(int a) {
  if (a == leader[a].first) return a;
  leader[a].first = findleader(leader[a].first);
  return leader[a].first;
}
int main() {
  infile.open("input.txt");
  outfile.open("output.txt");
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  ;
  int n, u, v, i, j, t1, t2;
  vector<pair<int, pair<int, int>>> storage;
  vector<bool> visited;
  vector<pair<int, int>> coords;
  cin >> n;
  coords.push_back(make_pair(0, 0));
  visited.push_back(0);
  leader.push_back(make_pair(0, 0));
  for (i = 1; i <= n; ++i) {
    cin >> u >> v;
    coords.push_back(make_pair(u, v));
    visited.push_back(0);
    leader.push_back(make_pair(0, 0));
  }
  for (i = 1; i <= n; ++i) {
    for (j = i + 1; j <= n; ++j) {
      storage.push_back(make_pair(min(abs(coords[i].first - coords[j].first),
                                      abs(coords[i].second - coords[j].second)),
                                  make_pair(i, j)));
    }
  }
  sort(storage.begin(), storage.end());
  long long sum = 0, counter = 0;
  for (i = 0; i < storage.size(); ++i) {
    if (counter == n) break;
    u = storage[i].second.first;
    v = storage[i].second.second;
    if (!leader[u].first) {
      if (!leader[v].first) {
        counter += 2;
        leader[u].first = v;
        leader[v].first = v;
        leader[v].second = 1;
      } else {
        ++counter;
        leader[u].first = v;
      }
      sum += storage[i].first;
    } else if (!leader[v].first) {
      ++counter;
      leader[v].first = u;
      sum += storage[i].first;
    } else {
      t1 = findleader(u);
      t2 = findleader(v);
      if (t1 != t2) {
        if (leader[t1].second == leader[t2].second) {
          ++leader[t1].second;
          leader[t2].first = t1;
        } else if (leader[t1].second > leader[t2].second) {
          leader[t2].first = t1;
        } else {
          leader[t1].first = t2;
        }
        sum += storage[i].first;
      }
    }
  }
  cout << sum;
}
