#include <bits/stdc++.h>
using namespace std;
struct edge {
  int cost;
  int from;
  int to;
  bool operator<(const edge& another) const { return cost < another.cost; }
};
vector<edge> edges;
int x[100001], y[100001];
bool connected[100001];
int main(void) {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d%d", x + i, y + i);
    connected[i] = false;
  }
  for (int i = 0; i < N; i++)
    for (int j = i + 1; j < N; j++) {
      edge e;
      e.cost = min(abs(x[i] - x[j]), abs(y[i] - y[j]));
      e.from = i;
      e.to = j;
      edges.push_back(e);
    }
  sort(edges.begin(), edges.end());
  long long ans = 0;
  int num = 0;
  for (int i = 0; i < edges.size(); i++) {
    edge e = edges[i];
    int from = e.from, to = e.to;
    if (connected[from] && connected[to]) continue;
    ans += e.cost;
    if (!connected[from]) {
      connected[from] = true;
      num++;
    }
    if (!connected[to]) {
      connected[to] = true;
      num++;
    }
    if (num == N) break;
  }
  return !printf("%lld\n", ans);
}
