#include <bits/stdc++.h>
long long int N;
std::vector<std::pair<long long int, long long int>> xy;
std::vector<long long int> res;
long long int ans;
int main() {
  std::cin >> N;
  for (int i = 0; i < N; ++i) {
    long long int x_, y_;
    std::cin >> x_ >> y_;
    xy.push_back({x_, y_});
  }
  xy.push_back({-11234567890, -11234567890});
  xy.push_back({11234567890, 11234567890});
  std::sort(xy.begin(), xy.end());
  for (int i = 1; i < xy.size() - 1; ++i) {
    auto x_num = xy[i + 1].first;
    auto y_num = xy[i + 1].second;
    auto next = std::min(abs(x_num - xy[i].first), abs(y_num - xy[i].second));
    x_num = xy[i - 1].first;
    y_num = xy[i - 1].second;
    auto prev = std::min(abs(x_num - xy[i].first), abs(y_num - xy[i].second));
    res.push_back(std::min(next, prev));
    if (next >= prev) {
      xy.erase(xy.begin() + i - 1);
    } else {
      xy.erase(xy.begin() + i);
    }
  }
  std::sort(res.begin(), res.end());
  for (int i = 0; i < res.size(); ++i) {
    ans += res[i];
  }
  std::cout << ans << std::endl;
  return 0;
}
