#include <bits/stdc++.h>
using namespace std;
struct st_city {
  int x;
  int y;
};
struct mv_cost {
  int city[2];
  int cost;
};
st_city city[100000] = {0};
vector<pair<int, int> > cost[100000];
bool through[100000];
bool mv_sort(const mv_cost& a, const mv_cost& b) { return a.cost < b.cost; }
int main(void) {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cost[i].clear();
    through[i] = false;
  }
  long long yen = 0;
  for (int i = 0; i < N; i++) {
    cin >> city[i].x >> city[i].y;
    for (int j = 0; j <= i; j++) {
      cost[j].push_back(make_pair(
          i, std::min(abs(city[i].x - city[j].x), abs(city[i].y - city[j].y))));
      cost[i].push_back(make_pair(
          j, std::min(abs(city[i].x - city[j].x), abs(city[i].y - city[j].y))));
    }
  }
  vector<mv_cost> mv;
  int step = 1;
  int reach = 1;
  through[reach] = true;
  while (step < N) {
    for (int i = 0; i < cost[reach].size(); i++) {
      if (!through[cost[reach].at(i).first]) {
        mv_cost m_cost;
        m_cost.city[0] = reach;
        m_cost.city[1] = cost[reach].at(i).first;
        m_cost.cost = cost[reach].at(i).second;
        mv.push_back(m_cost);
      }
    }
    std::sort(mv.begin(), mv.end(), mv_sort);
    for (unsigned int i = 0; i < mv.size(); i++) {
      if (!through[mv.at(i).city[0]])
        reach = mv.at(i).city[0];
      else if (!through[mv.at(i).city[1]])
        reach = mv.at(i).city[1];
      else
        continue;
      yen += mv.at(i).cost;
      break;
    }
    through[reach] = true;
    step++;
  }
  cout << yen << endl;
  return 0;
}
