#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = 1e17 - 10;
const double PI = acos(-1);
struct Tll {
  LL first, second, third;
};
LL h, w, n, m, s, k, ans, sum, last;
pair<LL, LL> a[200001];
struct Edge {
  LL cost;
  pair<LL, LL> pos;
};
int par[100001];
int find(int x) {
  if (par[x] == x) return x;
  par[x] = find(par[x]);
  return par[x];
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  par[x] = y;
}
bool same(int x, int y) { return find(x) == find(y); }
vector<Edge> vec;
string str, ss;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
int main() {
  for (int i = 0; i < 100001; i++) par[i] = i;
  cin >> n;
  for (LL i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    for (LL j = i - 1; j >= 0; j--) {
      vec.push_back(Edge{
          min(abs(a[i].first - a[j].first), abs(a[i].second - a[j].second)),
          pair<LL, LL>{i, j}});
    }
  }
  sort(vec.begin(), vec.end(), [](Edge x, Edge y) { return x.cost < y.cost; });
  for (LL i = 0; i < vec.size(); i++) {
    if (!same(vec[i].pos.first, vec[i].pos.second)) {
      ans += vec[i].cost;
      unite(vec[i].pos.first, vec[i].pos.second);
    }
  }
  cout << ans << endl;
  return 0;
}
