#include <bits/stdc++.h>
using namespace std;
struct edge {
  edge(int a, int b) {
    to = a;
    cost = b;
  }
  int to;
  int cost;
};
struct queInfo {
  queInfo(int a, int b) {
    idx = a;
    cost = b;
  }
  int idx;
  int cost;
};
bool cmpZahyou(pair<int, int> &a, pair<int, int> &b) {
  return a.second < b.second;
}
int main() {
  int N;
  vector<pair<int, int>> zahyouX;
  vector<pair<int, int>> zahyouY;
  cin >> N;
  for (int(i) = 0; (i) < (int)(N); ++(i)) {
    int x, y;
    cin >> x >> y;
    zahyouX.push_back(make_pair(i, x));
    zahyouY.push_back(make_pair(i, y));
  }
  static vector<edge> G[(100)];
  sort(zahyouX.begin(), zahyouX.end(), cmpZahyou);
  for (int(i) = 0; (i) < (int)(N - 1); ++(i)) {
    int prevIdx = zahyouX[i].first;
    int nextIdx = zahyouX[i + 1].first;
    int kyori = zahyouX[i + 1].second - zahyouX[i].second;
    G[prevIdx].push_back(edge(nextIdx, kyori));
    G[nextIdx].push_back(edge(prevIdx, kyori));
  }
  sort(zahyouY.begin(), zahyouY.end(), cmpZahyou);
  for (int(i) = 0; (i) < (int)(N - 1); ++(i)) {
    int prevIdx = zahyouY[i].first;
    int nextIdx = zahyouY[i + 1].first;
    int kyori = zahyouY[i + 1].second - zahyouY[i].second;
    G[prevIdx].push_back(edge(nextIdx, kyori));
    G[nextIdx].push_back(edge(prevIdx, kyori));
  }
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.push(make_pair(0, 0));
  unsigned long long int ans = 0;
  vector<bool> isUsed(N, false);
  vector<unsigned long> minCost(N, (0xFFFFFFFF));
  while (!que.empty()) {
    pair<int, int> point = que.top();
    int pointIdx = point.second;
    int pointCost = point.first;
    que.pop();
    if (isUsed[pointIdx]) {
      continue;
    }
    if (minCost[pointIdx] < pointCost) {
      continue;
    }
    ans += pointCost;
    isUsed[pointIdx] = true;
    for (auto egdeOne : G[pointIdx]) {
      int nextCost = egdeOne.cost;
      if (minCost[egdeOne.to] > nextCost) {
        minCost[egdeOne.to] = nextCost;
        que.push(make_pair(nextCost, egdeOne.to));
      }
    }
  }
  cout << ans;
  cout << endl;
  return 0;
}
