#include <bits/stdc++.h>
using namespace std;
int N;
int xs[100005];
int ys[100005];
int dis[100005];
int us[100005];
bool inside[100005] = {false};
int calDis(int i, int j) { return min(abs(xs[i] - xs[j]), abs(ys[i] - ys[j])); }
int updateDis(int idx) {
  int next = 0;
  inside[idx] = true;
  for (int i = 1; i <= N; ++i) {
    if (inside[i]) continue;
    if (calDis(idx, i) < dis[i]) {
      us[i] = idx;
      dis[i] = calDis(idx, i);
    }
    if (next == 0 || dis[i] < dis[next]) next = i;
  }
  return next;
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) {
    scanf("%d%d", &xs[i], &ys[i]);
    dis[i] = int(5 + 1e9);
  }
  int total = 0;
  int pre = 1;
  int next;
  for (int i = 0; i < N - 1; ++i) {
    next = updateDis(pre);
    int d = calDis(us[next], next);
    if (d != 0) {
      total += d;
    }
    pre = next;
  }
  printf("%d\n", total);
}
