#include <bits/stdc++.h>
using namespace std;
int INF = 1000000000;
int prim(int v, vector<vector<pair<int, int> > > G) {
  priority_queue<pair<int, int> > wait;
  int result = 0;
  vector<bool> connected(v, false);
  vector<int> cost(v, INF);
  wait.push(make_pair(0, 0));
  while (!wait.empty()) {
    int nowpoint = wait.top().second;
    int nowcost = -wait.top().first;
    wait.pop();
    if (!connected[nowpoint]) {
      result += nowcost;
      connected[nowpoint] = true;
      for (int i = 0; i < G[nowpoint].size(); ++i) {
        int nextpoint = G[nowpoint][i].second;
        int nextcost = G[nowpoint][i].first;
        if (nextcost < cost[nextpoint]) {
          wait.push(make_pair(-nextcost, nextpoint));
          cost[nextpoint] = nextcost;
        }
      }
    }
  }
  return result;
}
int main() {
  int N;
  cin >> N;
  int x[N], y[N];
  for (int i = 0; i < N; ++i) {
    cin >> x[i] >> y[i];
  }
  vector<vector<pair<int, int> > > G(N);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      G[i].push_back(make_pair(min(abs(x[i] - x[j]), abs(y[i] - y[j])), j));
    }
  }
  cout << prim(N, G) << endl;
  return 0;
}
