#include <bits/stdc++.h>
using namespace std;
typedef struct {
  long long x, y;
} City;
typedef struct {
  long long id, value;
} Node;
typedef struct {
  long long cost, id1, id2;
} Edge;
bool Node_asc(const Node& left, const Node& right) {
  return left.value < right.value;
}
bool Edge_asc(const Edge& left, const Edge& right) {
  return left.cost > right.cost;
}
int main() {
  long long N;
  vector<City> city;
  scanf("%lld", &N);
  for (int i = 0; i < N; i++) {
    City temp;
    scanf("%lld %lld", &temp.x, &temp.y);
    city.push_back(temp);
  }
  vector<Node> Xnode, Ynode;
  for (long long i = 0; i < N; i++) {
    Node temp;
    temp.id = i;
    temp.value = city.at(i).x;
    Xnode.push_back(temp);
    temp.value = city.at(i).y;
    Ynode.push_back(temp);
  }
  sort(Xnode.begin(), Xnode.end(), Node_asc);
  sort(Ynode.begin(), Ynode.end(), Node_asc);
  vector<Edge> edge;
  for (long long i = 0; i < N - 1; i++) {
    Edge temp;
    temp.cost = Xnode.at(i + 1).value - Xnode.at(i).value;
    temp.id1 = Xnode.at(i).id;
    temp.id2 = Xnode.at(i + 1).id;
    edge.push_back(temp);
    temp.cost = Ynode.at(i + 1).value - Ynode.at(i).value;
    temp.id1 = Ynode.at(i).id;
    temp.id2 = Ynode.at(i + 1).id;
    edge.push_back(temp);
  }
  sort(edge.begin(), edge.end(), Edge_asc);
  long long total = 0;
  set<long long> V;
  while (V.size() < N) {
    Edge temp = edge.back();
    edge.pop_back();
    if (V.count(temp.id1) && V.count(temp.id2)) continue;
    total += temp.cost;
    V.insert(temp.id1);
    V.insert(temp.id2);
  }
  printf("%lld\n", total);
  return 0;
}
