#include <bits/stdc++.h>
using namespace std;
const long long maxn = 200010, inf = LONG_LONG_MAX;
long long d[maxn], vis[maxn], x[maxn], y[maxn];
struct edge {
  long long to, len;
  edge(long long to, long long len) : to(to), len(len) {}
};
struct node {
  long long v, dist;
  node(long long x, long long y) : v(x), dist(y) {}
  friend bool operator<(node a, node b) { return a.dist > b.dist; }
};
vector<edge> G[maxn];
long long prim() {
  long long ans = 0;
  fill(vis, vis + maxn, 0);
  fill(d, d + maxn, inf);
  priority_queue<node> q;
  q.push(node(0, 0));
  while (q.size()) {
    node t = q.top();
    q.pop();
    if (vis[t.v]) continue;
    vis[t.v] = 1;
    ans += t.dist;
    for (long long i = 0; i < G[t.v].size(); i++) {
      edge e = G[t.v][i];
      if (vis[e.to]) continue;
      if (e.len < d[e.to]) d[e.to] = e.len, q.push(node(e.to, e.len));
    }
  }
  return ans;
}
struct point {
  long long x, y;
} p[maxn];
bool cmp(point a, point b) {
  if (a.x == b.x)
    return a.y < b.y;
  else
    return a.x < b.x;
}
bool cmpp(point a, point b) {
  if (a.y == b.y)
    return a.x < b.x;
  else
    return a.y < b.y;
}
map<pair<long long, long long>, long long> mp;
int main() {
  long long n;
  while (cin >> n) {
    mp.clear();
    for (long long i = 0; i < maxn; i++) G[i].clear();
    for (long long i = 0; i < n; i++)
      cin >> p[i].x >> p[i].y, mp[make_pair(p[i].x, p[i].y)] = i;
    sort(p, p + n, cmp);
    for (long long i = 1; i < n; i++) {
      long long u = mp[make_pair(p[i].x, p[i].y)],
                v = mp[make_pair(p[i - 1].x, p[i - 1].y)];
      long long len = min(abs(p[i].x - p[i - 1].x), abs(p[i].y - p[i - 1].y));
      G[u].push_back(edge(v, len));
      G[v].push_back(edge(u, len));
    }
    sort(p, p + n, cmpp);
    for (long long i = 1; i < n; i++) {
      long long u = mp[make_pair(p[i].x, p[i].y)],
                v = mp[make_pair(p[i - 1].x, p[i - 1].y)];
      long long len = min(abs(p[i].x - p[i - 1].x), abs(p[i].y - p[i - 1].y));
      G[u].push_back(edge(v, len));
      G[v].push_back(edge(u, len));
    }
    cout << prim() << endl;
  }
  return 0;
}
