#include <bits/stdc++.h>
using namespace std;
int par[1000000];
int find(int a);
void combine(int a, int b);
int main() {
  long long int N;
  cin >> N;
  int x[N], y[N];
  pair<int, pair<int, int>> root[(N * (N - 1)) / 2];
  int k, now, ans = 0;
  for (int i = 0; i < N; i++) {
    par[i] = i;
  }
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i];
  }
  k = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i; j++) {
      root[k].first = fmin(fabs(x[i] - x[j]), fabs(y[i] - y[j]));
      root[k].second.first = i;
      root[k].second.second = j;
      k++;
    }
  }
  sort(root, root + (N * (N - 1)) / 2);
  k = 0;
  now = 0;
  while (k < N - 1) {
    if (find(root[now].second.first) != find(root[now].second.second)) {
      combine(root[now].second.first, root[now].second.second);
      ans += root[now].first;
      k++;
    }
    now++;
  }
  cout << ans;
}
int find(int a) {
  int k;
  k = a;
  while (par[a] != a) {
    a = par[a];
  }
  par[k] = a;
  return a;
}
void combine(int a, int b) {
  int a1, b1;
  a1 = find(a);
  b1 = find(b);
  if (a1 != b1) {
    par[b1] = a1;
  }
}
