#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct UnionFind {
  static const int Max = 100010;
  int par[Max];
  int rank[Max];
  void init(int n) {
    for (int i = 0; i < n; ++i) {
      par[i] = i;
      rank[i] = 0;
    }
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
struct edge {
  int u, v;
  ll cost;
};
bool comp(const edge& e1, const edge& e2) { return e1.cost < e2.cost; }
int V;
vector<edge> es;
ll kruskal(vector<edge> es) {
  sort(es.begin(), es.end(), comp);
  UnionFind uf;
  uf.init(V);
  ll res = 0;
  for (ll i = 0; i < es.size(); ++i) {
    edge e = es[i];
    if (!uf.same(e.u, e.v)) {
      uf.unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}
int main() {
  cin >> V;
  using P = pair<int, int>;
  vector<P> v(V);
  for (int(i) = (0); (i) < (int)(V); ++(i)) {
    P a;
    int x, y;
    cin >> x >> y;
    a = {x, y};
    v[i] = a;
  }
  for (int(i) = (0); (i) < (int)(V); ++(i)) {
    for (int j = i + 1; j < V; ++j) {
      edge e = {i, j,
                min((int)abs(v[i].first - v[j].first),
                    (int)abs(v[i].second - v[j].second))};
      es.push_back(e);
    }
  }
  cout << kruskal(es) << endl;
}
