#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int posMat[N][2];
  for (int i = 0; i < N; i++) {
    cin >> posMat[i][0];
    cin >> posMat[i][1];
  }
  int minCost[N];
  fill(minCost, minCost + N, 999999999);
  minCost[0] = 0;
  bool used[N];
  fill(used, used + N, false);
  int totalCost = 0;
  for (int i = 0; i < N; i++) {
    int idx = -1;
    int minOfMin = 999999999;
    for (int j = 0; j < N; j++) {
      if (used[j] == false && minCost[j] < minOfMin) {
        idx = j;
        minOfMin = minCost[j];
      }
    }
    totalCost += minOfMin;
    used[idx] = true;
    for (int j = 0; j < N; j++) {
      if (used[j] == false) {
        int edgeCost = min(abs(posMat[idx][0] - posMat[j][0]),
                           abs(posMat[idx][1] - posMat[j][1]));
        minCost[j] = min(minCost[j], edgeCost);
      }
    }
  }
  cout << totalCost << endl;
  return 0;
}
