#include <bits/stdc++.h>
using namespace std;
long long n, x[100007], y[100007], xxx[100007], vtx[100007], yyy[100007],
    vty[100007], dd1[100007], hk[300007], kh[300007], kk[300000], u[300000],
    vtxu[300000], vtyu[300000];
void chatx(long a, long b) {
  int i, k, j, h, m;
  if (b - a >= 1) {
    i = a + ((b - a) / 2);
    chatx(a, i);
    chatx(i + 1, b);
    k = i + 1;
    h = a;
    j = a;
    for (j = a; j <= i; j++) {
      hk[j] = xxx[j];
      kh[j] = vtx[j];
    }
    for (j = a; j <= i; j++) {
      for (m = k; m <= b; m++)
        if (hk[j] > xxx[m]) {
          xxx[h] = xxx[m];
          vtx[h] = vtx[m];
          h++;
        } else
          break;
      k = m;
      xxx[h] = hk[j];
      vtx[h] = kh[j];
      h++;
    }
  }
}
void chaty(long a, long b) {
  int i, k, j, h, m;
  if (b - a >= 1) {
    i = a + ((b - a) / 2);
    chaty(a, i);
    chaty(i + 1, b);
    k = i + 1;
    h = a;
    j = a;
    for (j = a; j <= i; j++) {
      hk[j] = yyy[j];
      kh[j] = vty[j];
    }
    for (j = a; j <= i; j++) {
      for (m = k; m <= b; m++)
        if (hk[j] > yyy[m]) {
          yyy[h] = yyy[m];
          vty[h] = vty[m];
          h++;
        } else
          break;
      k = m;
      yyy[h] = hk[j];
      vty[h] = kh[j];
      h++;
    }
  }
}
void chat(long a, long b) {
  int i, k, j, h, m;
  if (b - a >= 1) {
    i = a + ((b - a) / 2);
    chat(a, i);
    chat(i + 1, b);
    k = i + 1;
    h = a;
    j = a;
    for (j = a; j <= i; j++) {
      hk[j] = u[j];
      kh[j] = vtxu[j];
      kk[j] = vtyu[j];
    }
    for (j = a; j <= i; j++) {
      for (m = k; m <= b; m++)
        if (hk[j] > u[m]) {
          u[h] = u[m];
          vtxu[h] = vtxu[m];
          vtyu[h] = vtyu[m];
          h++;
        } else
          break;
      k = m;
      u[h] = hk[j];
      vtxu[h] = kh[j];
      vtyu[h] = kk[j];
      h++;
    }
  }
}
int main() {
  cin >> n;
  long long i, j;
  for (i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
    xxx[i] = x[i];
    yyy[i] = y[i];
    vtx[i] = i;
    vty[i] = i;
  }
  long long mi;
  mi = 0;
  chatx(1, n);
  chaty(1, n);
  for (i = 1; i < n; i++) {
    mi++;
    u[mi] = abs(-x[vtx[i]] + x[vtx[i + 1]]);
    vtxu[mi] = vtx[i];
    vtyu[mi] = vtx[i + 1];
    mi++;
    u[mi] = abs(-y[vty[i]] + y[vty[i + 1]]);
    vtxu[mi] = vty[i];
    vtyu[mi] = vty[i + 1];
  }
  chat(1, mi);
  long long tong;
  tong = 0;
  for (i = 1; i <= mi; i++) {
    if (dd1[vtxu[i]] * dd1[vtyu[i]] == 0) {
      dd1[vtxu[i]] = 1;
      dd1[vtyu[i]] = 1;
      tong = tong + u[i];
    }
  }
  cout << tong;
}
