#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double pi = acos(-1);
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (ll i = (0), __last_i = (v.size()); i < __last_i; i++) {
    if (i) os << " ";
    os << v[i];
  }
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
  for (ll i = (0), __last_i = (v.size()); i < __last_i; i++) {
    if (i) os << endl;
    os << v[i];
  }
  return os;
}
class union_find {
 public:
  union_find(int n) : par_(n, -1) {}
  void init(int n) { par_.assign(n, -1); }
  int root(int x) { return par_[x] < 0 ? x : par_[x] = root(par_[x]); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    } else {
      if (par_[x] < par_[y]) {
        par_[x] += par_[y];
        par_[y] = x;
      } else {
        par_[y] += par_[x];
        par_[x] = y;
      }
      return true;
    }
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -par_[root(x)]; }

 private:
  std::vector<int> par_;
};
struct Kruskal {
  ll cost, from, to;
  bool operator<(const Kruskal& another) const { return cost < another.cost; }
};
int main() {
  ll n;
  ll x, y;
  ll cos;
  ll sum = 0;
  vector<pair<ll, ll>> v;
  vector<Kruskal> vv;
  cin >> n;
  union_find uf(n);
  for (ll i = (0), __last_i = (n); i < __last_i; i++) {
    cin >> x >> y;
    v.push_back({x, y});
  }
  for (ll i = (0), __last_i = (n); i < __last_i; i++) {
    for (ll j = (0), __last_j = (n); j < __last_j; j++) {
      cos = min(abs(v[i].first - v[j].first), abs(v[i].second - v[j].second));
      Kruskal kru = {cos, i, j};
      vv.push_back(kru);
    }
  }
  sort(vv.begin(), vv.end());
  for (ll i = (0), __last_i = (vv.size()); i < __last_i; i++) {
    if (uf.same(vv[i].from, vv[i].to) == false) {
      uf.unite(vv[i].from, vv[i].to);
      sum += vv[i].cost;
    }
  }
  cout << sum << endl;
}
