#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> x, y, dist;
unordered_set<int> nodes;
long long res;
void solve() {
  int mini = INT_MAX, candi;
  for (int i : nodes) {
    if (dist[i] < mini) {
      mini = dist[i];
      candi = i;
    }
  }
  res += mini;
  nodes.erase(candi);
  for (int i : nodes) {
    dist[i] = min(dist[i], min(abs(x[candi] - x[i]), abs(y[candi] - y[i])));
  }
}
int main() {
  cin >> n;
  x.resize(n), y.resize(n);
  dist.assign(n, INT_MAX);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i];
    nodes.insert(i);
  }
  res = 0;
  dist[0] = 0;
  for (int i = 0; i < n; ++i) {
    solve();
  }
  cout << res;
  return 0;
}
