#include <bits/stdc++.h>
using namespace std;
int Nmax = 100;
vector<tuple<int, int, int>> xbe;
vector<tuple<int, int, int>> ybe;
int n;
int counter = 0;
int result = 0;
struct graph {
  int name = 0;
  vector<pair<int, int>> nodes;
};
vector<graph*> nodes;
vector<graph*> con;
int use[10000];
int mst() {
  counter++;
  if (counter == n) return result;
  int x;
  int dist = 10e12;
  graph* node;
  for (int i = 0; i < con.size(); i++) {
    node = con[i];
    for (int j = 0; j < node->nodes.size(); j++) {
      if (use[get<0>(node->nodes[j])] != 1) {
        if (dist > get<1>(node->nodes[j])) {
          dist = get<1>(node->nodes[j]);
          x = get<0>(node->nodes[j]);
        }
      }
    }
  }
  use[x] = 1;
  result = result + dist;
  con.push_back(nodes[x]);
  return mst();
}
int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) use[i] = 0;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    xbe.push_back(tuple<int, int, int>(x, y, i));
    ybe.push_back(tuple<int, int, int>(y, x, i));
    graph* A = new graph;
    A->name = i;
    nodes.push_back(A);
  }
  sort(xbe.begin(), xbe.end());
  sort(ybe.begin(), ybe.end());
  for (int i = 0; i < n - 1; i++) {
    int x1, y1, x2, y2, i1, i2, dis;
    tuple<int, int, int> X1 = xbe[i];
    tuple<int, int, int> X2 = xbe[i + 1];
    tuple<int, int, int> Y1 = ybe[i];
    tuple<int, int, int> Y2 = ybe[i + 1];
    x1 = get<0>(X1);
    y1 = get<1>(X1);
    i1 = get<2>(X1);
    x2 = get<0>(X2);
    y2 = get<1>(X2);
    i2 = get<2>(X2);
    dis = min(abs(x2 - x1), abs(y2 - y1));
    nodes[i1]->nodes.push_back(pair<int, int>(i2, dis));
    nodes[i2]->nodes.push_back(pair<int, int>(i1, dis));
    x1 = get<0>(Y1);
    y1 = get<1>(Y1);
    i1 = get<2>(Y1);
    x2 = get<0>(Y2);
    y2 = get<1>(Y2);
    i2 = get<2>(Y2);
    dis = min(abs(x2 - x1), abs(y2 - y1));
    nodes[i1]->nodes.push_back(pair<int, int>(i2, dis));
    nodes[i2]->nodes.push_back(pair<int, int>(i1, dis));
  }
  con.push_back(nodes[0]);
  use[0] = 1;
  int result = mst();
  cout << result << "\n";
}
