#include <bits/stdc++.h>
const long long INF = 10000000000000000;
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long long> x(N), y(N);
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i];
  }
  vector<vector<int>> dist(N);
  int *d = new int[N];
  bool *vis = new bool[N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dist[i].push_back(min(abs(x[i] - x[j]), abs(y[i] - y[j])));
    }
  }
  for (int i = 0; i < N; i++) {
    d[i] = (int)1e9;
    vis[i] = false;
  }
  d[0] = 0;
  vis[0] = 0;
  int ans = 0;
  for (int i = 0; i < N; i++) {
    int current = 0;
    int current_d = int(1e9);
    for (int j = 0; j < N; j++) {
      if (current_d > d[j] && !vis[j]) {
        current_d = d[j];
        current = j;
      }
    }
    ans += current_d;
    vis[current] = true;
    for (int j = 0; j < N; j++) {
      if (dist[current][j] < d[j]) {
        d[j] = dist[current][j];
      }
    }
  }
  cout << ans << endl;
  delete[] d;
  delete[] vis;
  return 0;
}
