#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  pair<int, int> x_arr[N];
  pair<int, int> y_arr[N];
  for (int i = 0; i < N; i++) {
    int buf;
    cin >> buf;
    x_arr[i] = pair<int, int>(buf, i);
    cin >> buf;
    y_arr[i] = pair<int, int>(buf, i);
  }
  sort(x_arr, x_arr + N);
  sort(y_arr, y_arr + N);
  vector<pair<int, int> > edge[N];
  for (int i = 0; i < N - 1; i++) {
    int from = x_arr[i].second;
    int to = x_arr[i + 1].second;
    int cost = x_arr[i + 1].first - x_arr[i].first;
    edge[from].push_back(pair<int, int>(cost, to));
    edge[to].push_back(pair<int, int>(cost, from));
    from = y_arr[i].second;
    to = y_arr[i + 1].second;
    cost = y_arr[i + 1].first - y_arr[i].first;
    edge[from].push_back(pair<int, int>(cost, to));
    edge[to].push_back(pair<int, int>(cost, from));
  }
  int minCost[N];
  fill(minCost, minCost + N, 999999999);
  minCost[0] = 0;
  priority_queue<pair<int, int> > pq;
  pq.push(pair<int, int>(-0, 0));
  int totalCost = 0;
  while (!pq.empty()) {
    pair<int, int> p = pq.top();
    pq.pop();
    int u = p.second;
    if (minCost[u] < -p.first) continue;
    totalCost -= p.first;
    minCost[u] = -999999999;
    for (int i = 0; i < edge[u].size(); i++) {
      int v = edge[u][i].second;
      int c = edge[u][i].first;
      if (c < minCost[v]) {
        minCost[v] = c;
        pq.push(pair<int, int>(-c, v));
      }
    }
  }
  cout << totalCost << endl;
  return 0;
}
