#include <bits/stdc++.h>
using namespace std;
int N, par[100005], ranks[100005], a, b;
pair<int, int> town[100005];
long long res = 0;
struct Node {
  long long cost;
  int x;
  int y;
  Node(long long a, int b, int c) {
    cost = a;
    x = b;
    y = c;
  }
};
bool cmp(const Node& a, const Node& b) { return a.cost < b.cost; }
void init() {
  for (int i = 1; i <= N; i++) par[i] = i, ranks[i] = 0;
}
int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }
bool unite(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return false;
  if (ranks[x] < ranks[y])
    par[x] = y;
  else {
    par[y] = x;
    if (ranks[x] == ranks[y]) ranks[x]++;
  }
  return true;
}
vector<Node> e;
int main() {
  scanf("%d", &N);
  init();
  for (int i = 1; i <= N; i++) scanf("%d %d", &town[i].first, &town[i].second);
  for (int i = 1; i < N; i++)
    for (int j = i + 1; j <= N; j++) {
      long long k = (long long)min(abs(town[i].first - town[j].first),
                                   abs(town[i].second - town[j].second));
      e.push_back(Node(k, i, j));
    }
  sort(e.begin(), e.end(), cmp);
  for (int i = 0; i < e.size(); i++) {
    if (unite(e[i].x, e[i].y)) res += e[i].cost;
  }
  cout << res << endl;
  return 0;
}
