#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> a[10005];
vector<int> par;
vector<int> size;
void init(int n) {
  size.assign(n + 1, 1);
  par.assign(n + 1, 0);
  for (int i = 0; i <= n; i++) {
    par[i] = i;
  }
}
int find(int x) {
  if (par[x] == x) return par[x];
  par[x] = find(par[x]);
  return par[x];
}
bool issameset(int x, int y) { return find(x) == find(y); }
void Union(int x, int y) {
  if (!issameset(x, y)) {
    size[find(y)] += size[find(x)];
    par[find(x)] = find(y);
  }
}
int main() {
  cin >> n;
  init(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  vector<pair<int, pair<int, int> > > edgelist;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int w = min(abs(a[i].first - a[j].first), abs(a[i].second - a[j].second));
      edgelist.push_back(make_pair(w, pair<int, int>(i, j)));
    }
  }
  sort(edgelist.begin(), edgelist.end());
  int mst = 0;
  for (int i = 0; i < n * (n - 1) / 2; i++) {
    pair<int, pair<int, int> > front = edgelist[i];
    if (!issameset(front.second.first, front.second.second)) {
      Union(front.second.first, front.second.second);
      mst += front.first;
    }
  }
  cout << mst;
}
