#include <bits/stdc++.h>
using namespace std;
int par[100010];
int rank1[100010];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rank1[i] = 0;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  }
  if (rank1[x], rank1[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rank1[x] == rank1[y]) {
      rank1[x]++;
    }
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int main() {
  int n;
  cin >> n;
  init(n);
  vector<pair<int, int> > y(n);
  vector<pair<int, int> > x(n);
  for (int i = 1; i <= n; i++) {
    cin >> x[i - 1].first;
    cin >> y[i - 1].first;
    x[i - 1].second = i;
    y[i - 1].second = i;
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  vector<pair<int, pair<int, int> > > distance;
  for (int i = 0; i < n - 1; i++) {
    pair<int, pair<int, int> > in;
    in.second.first = x[i].second;
    in.second.second = x[i + 1].second;
    in.first = x[i + 1].first - x[i].first;
    distance.push_back(in);
    in.second.first = y[i].second;
    in.second.second = y[i + 1].second;
    in.first = y[i + 1].first - y[i].first;
    distance.push_back(in);
  }
  long long ans = 0;
  sort(distance.begin(), distance.end());
  for (int i = 1; i < n; i++) {
    while (same(distance[0].second.first, distance[0].second.second)) {
      distance.erase(distance.begin(), distance.begin() + 1);
    }
    ans += (long long)distance[0].first;
    unite(distance[0].second.first, distance[0].second.second);
    distance.erase(distance.begin(), distance.begin() + 1);
  }
  cout << ans << endl;
  return 0;
}
