#include <bits/stdc++.h>
using namespace std;
struct edge {
  int from, to;
  long long cost;
};
long long V, E;
vector<edge> es;
int par[100005];
int depth[100005];
bool mylesser(pair<pair<long long, long long>, int> a,
              pair<pair<long long, long long>, int> b) {
  return a.first.second < b.first.second;
}
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    depth[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 (depth[x] < depth[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (depth[x] == depth[y]) depth[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
bool comp(const edge& e1, const edge& e2) { return e1.cost < e2.cost; }
long long kruskal() {
  sort(es.begin(), es.end(), comp);
  init(V);
  long long res = 0;
  for (int i = 0; i < E; i++) {
    edge e = es[i];
    if (!same(e.from, e.to)) {
      res += e.cost;
      unite(e.from, e.to);
    }
  }
  return res;
}
int main() {
  scanf("%lld", &V);
  vector<pair<pair<int, int>, int> > p(V);
  for (int i = 0; i < V; i++) {
    scanf("%d %d", &p[i].first.first, &p[i].first.second);
    p[i].second = i;
  }
  sort(p.begin(), p.end());
  for (int i = 0; i < V - 1; i++) {
    pair<long long, long long> vvi = p[i].first, wwi = p[i + 1].first;
    long long cost = (long long)min(abs(vvi.first - wwi.first),
                                    abs(vvi.second - wwi.second));
    edge e = {p[i].second, p[i + 1].second, cost};
    es.push_back(e);
    swap(e.from, e.to);
    es.push_back(e);
  }
  sort(p.begin(), p.end(), mylesser);
  for (int i = 0; i < V - 1; i++) {
    pair<long long, long long> vvi = p[i].first, wwi = p[i + 1].first;
    long long cost = (long long)min(abs(vvi.first - wwi.first),
                                    abs(vvi.second - wwi.second));
    edge e = {p[i].second, p[i + 1].second, cost};
    es.push_back(e);
    swap(e.from, e.to);
    es.push_back(e);
  }
  E = 2 * (V - 1);
  printf("%lld\n", kruskal());
  return 0;
}
