#include "bits/stdc++.h"
#ifndef err
#define err(...)(void)0
#endif
using namespace std;
using ll = long long;
using ull = decltype(1ull);
template<class T>int size(T&&a) { return a.size(); }
#define REP(t,a)for(typename make_signed<decltype(a.second)>::type t=a.first,_l##t=a.second;t<_l##t;t++)
#define RREP(t,a)for(typename make_signed<decltype(a.second)>::type t=a.second-1,_l##t=a.first;t>=_l##t;t--)
template<class B>pair<int, B>make_pair(B b) { return{ 0,b }; }
#define rep(t,...)REP(t,make_pair(__VA_ARGS__))
#define rrep(t,...)RREP(t,make_pair(__VA_ARGS__))
#define all(a)begin(a),end(a)
#define rall(a)a.rbegin(),a.rend()
#define var(t, ...) t __VA_ARGS__;in,__VA_ARGS__;

void Calc();
int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  cin.exceptions(istream::failbit | istream::badbit);
  cout << fixed << setprecision(15); Calc(); cout.flush(); return 0;
}
template<class A>void amax(A&a, A b) { a = max(a, b); }
template<class A>void amin(A&a, A b) { a = min(a, b); }
template<class A>int comp(A a, A b) { return a < b ? 1 : b < a ? -1 : 0; }
struct Scanner {
  template<class A = string>A Next() { A a; cin >> a; return a; }
  template<class A = int>vector<A>Array(int n) { vector<A>a(n); for (A&i : a) cin >> i; return a; }
  string Line() const { string s; getline(cin, s); return s; }
  template<class A>Scanner&operator,(A&a) { a = Next<A>(); return *this; }
  template<class A>operator A() { return Next<A>(); }
}in;

/*---------------------------------------------------------------------*/



void Calc() {

  int n = in;
  struct Data :pair<int, int> {
    int index;
    int c = 0;
  };
  vector<Data> p(n);
  for (auto& i : p) {
    in, i.first, i.second;
  }
  rep(i, n)p[i].index = i;


  p[0].c = 1;
  ll ans = 0;
  auto f2 = [](auto a, auto b, auto c, auto d) {
    return min(abs(a - c), abs(b - d));
  };

  sort(all(p), [&](auto a, auto b) {
    return f2(p[0].first, p[0].second, a.first, a.second)
      < f2(p[0].first, p[0].second, b.first, b.second);
  });
  err(p);
  set<int> x, y;

  x.insert(p[0].first);
  y.insert(p[0].second);
  rep(i, 1, n) {
    auto t = (int) 1e9 + 10;
    err(t);

    auto it = x.lower_bound(p[i].first);
    if (it != x.end()) {
      amin(t, abs(p[i].first - *it));
    }
    if (it != x.begin()) --it;
    if (it != x.end()) amin(t, abs(p[i].first - *it));

    it = y.lower_bound(p[i].second);
    if (it != y.end()) {
      amin(t, abs(p[i].second - *it));
    }
    if (it != y.begin()) --it;
    if (it != y.end()) amin(t, abs(p[i].second - *it));
    ans += t;
    x.insert(p[i].first);
    y.insert(p[i].second);

  }
  cout << (ans) << endl;

}
