#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
struct edge {
  int from, to, cost;
  edge(int _from, int _to, int _cost) : from(_from), to(_to), cost(_cost) {}
  bool operator<(const edge &other) const { return cost < other.cost; }
};
vector<edge> es;
int father[maxn];
pair<int, int> p[maxn];
int find(int x) {
  if (x == father[x]) {
    return father[x];
  } else {
    return father[x] = find(father[x]);
  }
}
int unite(int x, int y) {
  x = find(x);
  y = find(y);
  father[x] = y;
}
long long kruskal() {
  sort(es.begin(), es.end());
  long long res = 0;
  for (int i = 0; i < es.size(); i++) {
    if (find(es[i].from) != find(es[i].to)) {
      unite(es[i].from, es[i].to);
      res += es[i].cost;
    }
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i].first >> p[i].second;
    for (int j = i - 1; j >= 1; j--) {
      es.push_back(edge(
          i, j,
          min(abs(p[i].first - p[j].first), abs(p[i].second - p[j].second))));
    }
  }
  for (int i = 1; i <= n; i++) {
    father[i] = i;
  }
  cout << kruskal() << endl;
  return 0;
}
