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