#include <bits/stdc++.h>
using namespace std;
typedef struct tip {
  int a, b, c;
};
tip v[100005];
vector<int> L[100005];
bool cmp(tip a, tip b) { return a.a < b.a; }
bool cmp2(tip a, tip b) { return a.b < b.b; }
int dy;
int n;
bool seen[100005];
int dp[100005];
long long sol;
inline int dist(int i, int j) {
  return min(abs(v[j].a - v[i].a), abs(v[i].b - v[j].b));
}
typedef struct edge {
  int nod;
  int cost;
};
bool operator<(const edge& a, const edge& b) { return a.cost > b.cost; }
priority_queue<edge> q;
void BuildAPM() {
  int nodes = 0;
  while (nodes < n) {
    int nod = q.top().nod;
    int cost = q.top().cost;
    q.pop();
    if (cost > dp[nod]) continue;
    sol += 1LL * cost;
    seen[nod] = 1;
    nodes++;
    for (vector<int>::iterator it = L[nod].begin(); it != L[nod].end(); it++) {
      if (!seen[*it] && dp[*it] > (dist(nod, *it))) {
        dp[*it] = dist(nod, *it);
        q.push({*it, dp[*it]});
      }
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i].a >> v[i].b;
    v[i].c = i;
  }
  sort(v + 1, v + n + 1, cmp);
  for (int i = 1; i < n; i++) {
    L[v[i].c].push_back(v[i + 1].c);
    L[v[i + 1].c].push_back(v[i].c);
  }
  sort(v + 1, v + n + 1, cmp2);
  for (int i = 1; i < n; i++) {
    L[v[i].c].push_back(v[i + 1].c);
    L[v[i + 1].c].push_back(v[i].c);
  }
  for (int i = 2; i <= n; i++) dp[i] = INT_MAX;
  q.push({1, 0});
  BuildAPM();
  printf("%lld\n", sol);
  return 0;
}
