#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7, INF = 1e9;
using vi = vector<int>;
using vvi = vector<vi>;
struct edge {
  int u, v, cost;
};
bool comp(const edge& e1, const edge& e2) { return e1.cost < e2.cost; }
int tree[100010];
int root(int x) { return tree[x] == x ? x : tree[x] = root(tree[x]); }
void unite(int a, int b) { tree[root(a)] = root(b); }
edge es[100010];
int V, E;
long long kruskal() {
  sort(es, es + E, comp);
  for (int i = 0; i < V; i++) tree[i] = i;
  long long res = 0;
  for (int i = 0; i < E; i++) {
    edge e = es[i];
    if (root(e.u) != root(e.v)) {
      unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}
signed main() {
  cin >> V;
  struct zahyou {
    int x, y;
  };
  zahyou za[100010];
  for (int i = 0; i < V; i++) cin >> za[i].x >> za[i].y;
  int koko = 0;
  for (int i = 0; i < V; i++) {
    for (int j = i; j < V; j++) {
      es[koko].u = i;
      es[koko].v = j;
      es[koko].cost = min(abs(za[i].x - za[j].x), abs(za[i].y - za[j].y));
      koko++;
    }
  }
  E = koko;
  cout << kruskal() << endl;
  return 0;
}
