#include <bits/stdc++.h>
const int INF = 1e9;
const long long LLINF = 1e18;
using namespace std;
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int N, x, y;
vector<pair<int, int> > xdist;
vector<pair<int, int> > ydist;
vector<pair<int, pair<int, int> > > edge;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x >> y;
    xdist.push_back(make_pair(x, i));
    ydist.push_back(make_pair(y, i));
  }
  sort(xdist.begin(), xdist.end());
  sort(ydist.begin(), ydist.end());
  for (int i = 0; i < N - 1; i++) {
    edge.push_back(make_pair(xdist[i + 1].first - xdist[i].first,
                             make_pair(xdist[i].second, xdist[i + 1].second)));
    edge.push_back(make_pair(ydist[i + 1].first - ydist[i].first,
                             make_pair(ydist[i].second, ydist[i + 1].second)));
  }
  sort(edge.begin(), edge.end());
  long long ans = 0;
  set<int> g;
  int i = 0;
  while (g.size() < N) {
    int n0 = edge[i].second.first;
    int n1 = edge[i].second.second;
    if (g.find(n0) == g.end() || g.find(n1) == g.end()) {
      ans += (long long)edge[i].first;
      g.insert(n0);
      g.insert(n1);
    }
    i++;
  }
  cout << ans << endl;
  return 0;
}
