#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int x, y;
} ss;
ss team[200000];
vector<int> team2[200000];
int cmp(const void *a, const void *b) {
  ss *c = (ss *)a, *d = (ss *)b;
  if ((c->x) != (d->x)) return (c->x) - (d->x);
  return (c->y) - (d->y);
}
int main() {
  int n;
  int dis[200000] = {0};
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d %d", &team[i].x, &team[i].y);
  qsort(team + 1, n, sizeof(team[1]), cmp);
  dis[0] = team[1].x;
  team2[0].push_back(team[1].y);
  int c1 = 0;
  for (int i = 2; i <= n; i++) {
    if (team[i].x != team[i - 1].x) c1++;
    dis[c1] = team[i].x;
    team2[c1].push_back(team[i].y);
  }
  long long ans = 0;
  for (int i = 1; i <= c1; i++) {
    long long now = dis[i] - dis[i - 1];
    for (int j = 0; j < team2[i].size(); j++) {
      int index =
          lower_bound(team2[i - 1].begin(), team2[i - 1].end(), team2[i][j]) -
          team2[i - 1].begin();
      if (index - 1 >= 0)
        now = min(now, (long long)abs(team2[i][j] - team2[i - 1][index - 1]));
      if (index < team2[i - 1].size())
        now = min(now, (long long)abs(team2[i - 1][index] - team2[i][j]));
    }
    ans += now;
  }
  printf("%lld", ans);
  return 0;
}
