#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, x[N], y[N];
struct Edge {
  int u, v, w;
  Edge() {}
  Edge(int u, int v, int w) : u(u), v(v), w(w) {}
  bool operator<(const Edge &rhs) const { return w < rhs.w; }
};
vector<Edge> v;
int fa[N];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", x + i, y + i);
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      v.push_back(Edge(i, j, min(abs(x[i] - x[j]), abs(y[i] - y[j]))));
  sort(v.begin(), v.end());
  for (int i = 1; i <= n; i++) fa[i] = i;
  int cnt = 0, res = 0;
  for (vector<Edge>::iterator it = v.begin(); it != v.end(); ++it) {
    int tx = find(it->u), ty = find(it->v);
    if (tx != ty) {
      fa[ty] = tx;
      res += it->w;
      if (++cnt == n - 1) break;
    }
  }
  printf("%d\n", res);
  return 0;
}
