#include <bits/stdc++.h>
using namespace std;
void PrimMST(int N, vector<pair<int, int>> coords) {
  vector<int> lowcost(N);
  vector<int> closest(N);
  vector<bool> s(N);
  s[0] = true;
  for (int i = 0; i < N; ++i) {
    lowcost[i] = min(abs(coords[0].first - coords[i].first),
                     abs(coords[0].second - coords[i].second));
    closest[i] = 0;
    s[i] = false;
  }
  int min;
  int minid;
  int total_weight = 0;
  for (int i = 0; i < N - 1; ++i) {
    min = INT_MAX;
    minid = -1;
    for (int k = 1; k < N; ++k) {
      if ((lowcost[k] < min) && (!s[k])) {
        min = lowcost[k];
        minid = k;
      }
    }
    total_weight += min;
    s[minid] = true;
    for (int k = 1; k < N; ++k) {
      if ((std::min(abs(coords[minid].first - coords[k].first),
                    abs(coords[minid].second - coords[k].second)) <
           lowcost[k]) &&
          (!s[k])) {
        lowcost[k] = std::min(abs(coords[minid].first - coords[k].first),
                              abs(coords[minid].second - coords[k].second));
        closest[k] = minid;
      }
    }
  }
  cout << total_weight << endl;
}
int main() {
  int N;
  int xi, yi;
  vector<pair<int, int>> coords;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> xi >> yi;
    coords.push_back(make_pair(xi, yi));
  }
  PrimMST(N, coords);
}
