#include <bits/stdc++.h>
using namespace std;
namespace {
struct input_returnner {
  long long N;
  input_returnner(long long N_ = 0) : N(N_) {}
  template <typename T>
  operator vector<T>() const {
    vector<T> res(N);
    for (auto& a : res) cin >> a;
    return std::move(res);
  }
  template <typename T>
  operator T() const {
    T res;
    cin >> res;
    return res;
  }
  template <typename T>
  T operator-(T right) {
    return T(input_returnner()) - right;
  }
  template <typename T>
  T operator+(T right) {
    return T(input_returnner()) + right;
  }
  template <typename T>
  T operator*(T right) {
    return T(input_returnner()) * right;
  }
  template <typename T>
  T operator/(T right) {
    return T(input_returnner()) / right;
  }
  template <typename T>
  T operator<<(T right) {
    return T(input_returnner()) << right;
  }
  template <typename T>
  T operator>>(T right) {
    return T(input_returnner()) >> right;
  }
};
template <typename T>
input_returnner in() {
  return in<T>();
}
input_returnner in() { return input_returnner(); }
input_returnner in(long long N) { return std::move(input_returnner(N)); }
}  // namespace
const long long MOD = 1e9 + 7;
struct ModInt {
  long long v = 0;
  ModInt() {}
  template <class T>
  ModInt(const T& right) {
    v = right;
    if (v >= 0)
      v %= MOD;
    else
      v += ((-v) / MOD + 1) * MOD;
    v %= MOD;
  }
  void operator=(const ModInt& right) { v = right.v; }
  template <class T>
  void operator=(const T& right) {
    v = right;
    if (v >= 0)
      v %= MOD;
    else
      v = v += ((-v) / MOD + 1) * MOD;
    v %= MOD;
  }
  ModInt operator+(const ModInt& right) { return (v + right.v) % MOD; }
  ModInt operator-(const ModInt& right) { return (MOD - (v - right.v)); }
  ModInt operator*(const ModInt& right) { return (v * right.v) % MOD; }
  ModInt operator/(const ModInt& right) { return v / right.v; }
  void operator+=(const ModInt& right) { v = (v + right.v) % MOD; }
  void operator-=(const ModInt& right) { v = (MOD - (v - right.v)); }
  void operator*=(const ModInt& right) { v = (v * right.v) % MOD; }
  void operator/=(const ModInt& right) { v = v / right.v; }
  bool operator==(const ModInt& right) { return v == right.v; }
};
ostream& operator<<(ostream& os, const ModInt& value) {
  os << value.v;
  return os;
}
void solve();
signed main(void) {
  cin.tie(0), ios::sync_with_stdio(false), cout << setprecision(15);
  solve();
  return 0;
}
struct Edge {
  int f, t, c;
  Edge(int f_, int t_, int c_) : f(f_), t(t_), c(c_) {}
  Edge() {}
};
template <class T>
struct vector2 {
  T x;
  T y;
  vector2(T x_, T y_) : x(x_), y(y_) {}
  vector2() : x(0), y(0) {}
  vector2 operator+(const vector2<T>& right) {
    return vector2(x + right.x, y + right.y);
  }
  vector2 operator-(const vector2<T>& right) {
    return vector2(x - right.x, y - right.y);
  }
  T dot(const vector2<T>& right) { return x * right.x + y * right.y; }
  T det(const vector2<T>& right) { return (x * right.y) + (-y * right.x); }
};
class union_find {
 private:
  vector<long long> par;
  vector<long long> rank;
  vector<long long> count;

 public:
  union_find(long long N) : par(N), rank(N, 0), count(N, 1) {
    for (long long i = 0; i < N; ++i) {
      par[i] = i;
    }
  }
  long long find(long long x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  void unite(long long x, long long y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
      count[y] += count[x];
      par[x] = y;
    } else {
      count[x] += count[y];
      par[y] = x;
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
  bool same(long long x, long long y) { return find(x) == find(y); }
  long long getCount(long long x) { return count[find(x)]; }
  void clean() { par = vector<long long>(par.size()); }
};
void solve() {
  long long N;
  cin >> N;
  vector<pair<pair<long long, long long>, long long>> P(N);
  for (long long i = 0; i < (N); i++) {
    cin >> P[i].first.first >> P[i].first.second;
    P[i].second = i;
  }
  using mytype = pair<long long, pair<long long, long long>>;
  priority_queue<mytype, vector<mytype>, greater<mytype>> q;
  sort((P).begin(), (P).end());
  for (long long i = 0; i < (N - 1); i++) {
    q.push(make_pair(P[i + 1].first.first - P[i].first.first,
                     make_pair(P[i].second, P[i + 1].second)));
  }
  sort((P).begin(), (P).end(),
       [](const pair<pair<long long, long long>, long long>& left,
          const pair<pair<long long, long long>, long long>& right) -> bool {
         if (left.first.second != right.first.second) {
           return left.first.second < right.first.second;
         } else
           left < right;
       });
  for (long long i = 0; i < (N - 1); i++) {
    q.push(make_pair(P[i + 1].first.second - P[i].first.second,
                     make_pair(P[i].second, P[i + 1].second)));
  }
  union_find uf(N + 1);
  long long res = 0;
  while (not q.empty()) {
    auto a = q.top();
    q.pop();
    if (not uf.same(a.second.first, a.second.second)) {
      uf.unite(a.second.first, a.second.second);
      res += a.first;
    }
  }
  cout << res << endl;
}
