#include <bits/stdc++.h>
using namespace std;
int ufind(vector<int>& P, int u) {
  if (P[u] == 0) {
    return u;
  }
  return ufind(P, P[u]);
}
void uf(vector<int>& P, int u, int v) {
  int _u = ufind(P, u);
  int _v = ufind(P, v);
  if (_u != _v) {
    P[_u] = _v;
  }
}
int main() {
  int n;
  vector<pair<int, int> > X, Y;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int x, y;
    cin >> x >> y;
    X.push_back(make_pair(x, i));
    Y.push_back(make_pair(y, i));
  }
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  priority_queue<pair<int, pair<int, int> >,
                 vector<pair<int, pair<int, int> > >,
                 greater<pair<int, pair<int, int> > > >
      q;
  for (int i = 1; i < n; ++i) {
    int u = X[i - 1].second, v = X[i].second;
    int xu = X[i - 1].first, xv = X[i].first;
    q.push(make_pair(xv - xu, make_pair(u, v)));
  }
  for (int i = 1; i < n; ++i) {
    int u = Y[i - 1].second, v = Y[i].second;
    int yu = Y[i - 1].first, yv = Y[i].first;
    q.push(make_pair(yv - yu, make_pair(u, v)));
  }
  long long res = 0;
  vector<int> P(n + 1);
  while (!q.empty()) {
    int d = q.top().first;
    int u = q.top().second.first, v = q.top().second.second;
    q.pop();
    if (ufind(P, u) != ufind(P, v)) {
      res += d;
      uf(P, u, v);
    }
  }
  cout << res << "\n";
}
