#include <bits/stdc++.h>
using namespace std;
const int nmax = 100100;
int x[nmax], y[nmax];
int indices[nmax];
int r[nmax];
int N;
vector<pair<long long, pair<int, int> > > edges;
int root(int index) { return (r[index] == index) ? index : (root(r[index])); }
void read() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> x[i] >> y[i];
    indices[i] = i;
    r[i] = i;
  }
  sort(indices, indices + N, [](int a, int b) { return x[a] < x[b]; });
  for (int i = 0; i < N - 1; ++i) {
    edges.push_back(make_pair(x[indices[i + 1]] - x[indices[i]],
                              make_pair(indices[i + 1], indices[i])));
  }
  sort(indices, indices + N, [](int a, int b) { return y[a] < y[b]; });
  for (int i = 0; i < N - 1; ++i) {
    edges.push_back(make_pair(y[indices[i + 1]] - y[indices[i]],
                              make_pair(indices[i + 1], indices[i])));
  }
  sort(edges.begin(), edges.end());
  long long ans = 0;
  for (int i = 0; i < edges.size(); ++i) {
    if (root(r[edges[i].second.first]) == root(r[edges[i].second.second]))
      continue;
    ans += edges[i].first;
    r[root(edges[i].second.first)] = root(edges[i].second.second);
  }
  cout << ans << endl;
}
void solve() {}
int main() {
  std::ios::sync_with_stdio(false);
  read();
  solve();
  return 0;
}
