#include <bits/stdc++.h>
using namespace std;
pair<int, int> v[100005], y[100005];
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].first - v[i].first), abs(v[i].second - v[j].second));
}
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 (int i = 1; i <= n; i++) {
      if (!seen[i] && dp[i] > (dist(nod, i))) {
        dp[i] = dist(nod, i);
        q.push({i, dp[i]});
      }
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i].first >> v[i].second;
  }
  sort(v + 1, v + n + 1);
  for (int i = 1; i < n; i++) {
    if (!(v[i].first == v[i + 1].first && v[i].second == v[i + 1].second))
      y[++dy] = v[i];
  }
  y[++dy] = v[n];
  n = dy;
  for (int i = 1; i <= n; i++) {
    v[i] = y[i];
  }
  for (int i = 2; i <= n; i++) dp[i] = INT_MAX;
  q.push({1, 0});
  BuildAPM();
  printf("%lld\n", sol);
  return 0;
}
