#include <bits/stdc++.h>
using namespace std;
int N, l[200005], r[200005], res = 0, par[200005], ranks[200005];
struct Node {
  int w, x, y;
  Node(int a, int b, int c) { w = a, x = b, y = c; }
};
vector<Node> e;
inline bool cmp(Node a, Node b) { return a.w < b.w; }
void Init() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) scanf("%d %d", &l[i], &r[i]);
  for (int i = 1; i < N; i++)
    for (int j = i + 1; j <= N; j++) {
      Node tmp(min(abs(l[i] - l[j]), abs(r[i] - r[j])), i, j);
      e.emplace_back(tmp);
    }
  sort(e.begin(), e.end(), cmp);
}
void init() {
  for (int i = 1; i <= N; i++) par[i] = i, ranks[i] = 0;
}
int find(int x) { return x == par[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]) swap(x, y);
  par[y] = x;
  if (ranks[x] == ranks[y]) ranks[x]++;
  return true;
}
int main() {
  Init();
  init();
  for (int i = 0; i < e.size(); i++) res += unite(e[i].x, e[i].y) * e[i].w;
  printf("%d\n", res);
  return 0;
}
