#include <bits/stdc++.h>
using namespace std;
pair<int, int> p[100000];
bool b[100000];
int main() {
  int n, x, y;
  cin >> n;
  for (int(i) = 0; (i) < (n); (i)++) {
    cin >> x >> y;
    p[i].first = x;
    p[i].second = y;
  }
  long long sum = 0;
  int j = 0;
  int c = 0;
  bool l = false;
  int mi = 0;
  for (int(i) = 0; (i) < (n); (i)++) {
    if (b[i]) continue;
    if (i == n - 1 && l) {
      continue;
    }
    b[i] = true;
    int mn = 2000000000;
    for (int(j) = 0; (j) < (n); (j)++) {
      if (i == j) continue;
      int f = min(abs(p[i].first - p[j].first), abs(p[i].second - p[j].second));
      if (mn > f) {
        mn = f;
        mi = j;
      }
    }
    sum += mn;
    b[mi] = true;
  }
  cout << sum << endl;
  return 0;
}
