import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;


void main()
{
  int n;
  readf("%s\n", &n);

  auto p = new int[][](n, 2);
  foreach (i; 0..n) {
    p[i] = readln.chomp.split.to!(int[]);
  }

  int getCost(int i, int j) {
    return min(
      max(p[i][0], p[j][0]) - min(p[i][0], p[j][0]), 
      max(p[i][1], p[j][1]) - min(p[i][1], p[j][1])
    );
  }

  auto d = new int[](n);
  d[0] = 10^^9 + 1;
  foreach (i; 1..n) {
    d[i] = getCost(0, i);
  }

  auto cnt = 1;
  long cost;
  while (cnt < n) {
    int si, sc = int.max;
    foreach (i; 1..n) {
      if (d[i] < sc) {
        sc = d[i];
        si = i; 
      }
    }
    cnt++;
    cost += sc;
    d[si] = 10^^9 + 1;
    foreach (i; 1..n) {
      if (d[i] == 10^^9 + 1) continue;
      d[i] = min(getCost(si, i), d[i]);
    }
  }
  cost.writeln;
}
