#include <bits/stdc++.h>
using namespace std;
vector<long long int> x(1e5 + 5);
vector<long long int> y(1e5 + 5);
struct Edge {
  long long int to;
  long long int cost;
  bool operator<(const Edge &r) const { return cost > r.cost; }
};
long long int dijkstra(long long int n) {
  set<long long int> nodes;
  nodes.insert(0);
  long long int sum = 0;
  priority_queue<Edge> que;
  for (long long int to = 0; to < (n); to++) {
    if (to == 0) continue;
    long long int xCost = abs(x[0] - x[to]);
    long long int yCost = abs(y[0] - y[to]);
    que.push({to, min(xCost, yCost)});
  }
  long long int len = 1;
  while (len < n) {
    Edge edge = que.top();
    que.pop();
    if (0 < nodes.count(edge.to)) continue;
    nodes.insert(edge.to);
    len++;
    for (long long int to = 0; to < (n); to++) {
      if (to == edge.to) continue;
      long long int xCost = abs(x[edge.to] - x[to]);
      long long int yCost = abs(y[edge.to] - y[to]);
      que.push({to, min(xCost, yCost)});
    }
    sum += edge.cost;
  }
  return sum;
}
int main() {
  long long int n;
  cin >> n;
  for (long long int i = 0; i < (n); i++) cin >> x[i] >> y[i];
  cout << dijkstra(n) << endl;
  return 0;
}
