#include <bits/stdc++.h>
using namespace std;
bool vis[100005];
int n, dis, x[100005], y[100005], t, ans;
pair<int, pair<int, int> > tmp;
priority_queue<pair<int, pair<int, int> > > q;
int Abs(int ooo) { return ooo >= 0 ? ooo : -ooo; }
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &x[i], &y[i]);
  }
  for (int i = 1; i < n; i++) {
    dis = min(Abs(x[i] - x[0]), Abs(y[i] - y[0]));
    q.push(make_pair((-dis), (make_pair((0), (i)))));
  }
  vis[0] = true;
  while (!q.empty()) {
    tmp = q.top();
    q.pop();
    if (vis[tmp.second.second]) continue;
    ans += -tmp.first;
    t++;
    if (t == n - 1) break;
    vis[tmp.second.second] = true;
    for (int i = 1; i < n; i++) {
      if (vis[i]) continue;
      dis = min(Abs(x[i] - x[tmp.second.second]),
                Abs(y[i] - y[tmp.second.second]));
      q.push(make_pair((-dis), (make_pair((tmp.second.second), (i)))));
    }
  }
  printf("%d", ans);
}
