#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  pair<int, int> p[n + 1];
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    p[i] = make_pair(x, y);
  }
  int visited[n + 1], d[n + 1];
  for (int i = 1; i <= n; i++) {
    visited[i] = 0;
    d[i] = 2000000000;
  }
  d[1] = 0;
  int sum = 0;
  while (1) {
    int mincost = 2000000000;
    int u;
    for (int i = 1; i <= n; i++) {
      if (visited[i] == 0 && d[i] < mincost) {
        mincost = d[i];
        u = i;
      }
    }
    if (mincost == 2000000000) break;
    sum = sum + mincost;
    visited[u] = 1;
    for (int v = 1; v <= n; v++) {
      if (visited[v] == 0) {
        int cost =
            min(abs(p[u].first - p[v].first), abs(p[u].second - p[v].second));
        if (cost < d[v]) d[v] = cost;
      }
    }
  }
  cout << sum << endl;
}
