#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int>> px, py;
vector<vector<pair<int, int>>> a;
vector<bool> b;
vector<vector<int>> s;
bool sort2(pair<int, int> x, pair<int, int> y) { return x.second < y.second; }
bool sorts(vector<int> x, vector<int> y) { return x[2] < y[2]; }
void runmst() {
  int res = 0;
  sort(s.begin(), s.end(), sorts);
  b.assign(n, 0);
  for (int i = 0; i < s.size(); i++) {
    int x = s[i][0], y = s[i][1], val = s[i][2];
    if (!b[x] || !b[y]) {
      res += val;
      b[x] = 1;
      b[y] = 1;
    }
  }
  printf("%d", res);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    px.push_back(pair<int, int>(x, i));
    py.push_back(pair<int, int>(y, i));
  }
  a.assign(n, vector<pair<int, int>>());
  sort(px.begin(), px.end());
  sort(py.begin(), py.end());
  for (int i = 0; i < n - 1; i++) {
    s.push_back(vector<int>());
    s[i * 2].push_back(px[i].second);
    s[i * 2].push_back(px[i + 1].second);
    s[i * 2].push_back(px[i + 1].first - px[i].first);
    s.push_back(vector<int>());
    s[i * 2 + 1].push_back(py[i].second);
    s[i * 2 + 1].push_back(py[i + 1].second);
    s[i * 2 + 1].push_back(py[i + 1].first - py[i].first);
  }
  runmst();
}
