// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

using pii = pair<int, int>;

using ppii = pair<int, pii>;



int n;

vector<pair<int, pii>> a;

vector<pii> edge[100010];



bool less_x(ppii s, ppii t) {

  return s.second.first < t.second.first;

}



bool less_y(ppii s, ppii t) {

  return s.second.second < t.second.second;

}



long long prim() {

  priority_queue<pii, vector<pii>, greater<pii>> q;

  vector<bool> visited(n, false);

  q.push(make_pair(0, 0));

  long long ans = 0;

  while (!q.empty()) {

    auto p = q.top();

    q.pop();

    int cost = p.first, u = p.second;

    if (visited[u]) {

      continue;

    }

    visited[u] = true;

    ans += cost;

    for (auto e: edge[u]) {

      int v = e.first, c = e.second;

      if (visited[v]) {

        continue;

      }

      q.push(make_pair(c, v));

    }

  }

  return ans;

}



int main() {

  ios::sync_with_stdio(false);

  cin.tie(0);

  cin >> n;

  a = vector<ppii>(n);

  for (int i = 0; i < n; i++) {

    int x, y;

    cin >> x >> y;

    a[i] = make_pair(i, make_pair(x, y));

  }

  sort(a.begin(), a.end(), less_x);

  auto p0 = a[0];

  for (int i = 1; i < n; i++) {

    auto p1 = a[i];

    int cost = min(abs(p0.second.first - p1.second.first), abs(p0.second.second - p1.second.second));

    edge[p0.first].push_back(make_pair(p1.first, cost));

    edge[p1.first].push_back(make_pair(p0.first, cost));

    p0 = p1;

  }

  sort(a.begin(), a.end(), less_y);

  p0 = a[0];

  for (int i = 1; i < n; i++) {

    auto p1 = a[i];

    int cost = min(abs(p0.second.first - p1.second.first), abs(p0.second.second - p1.second.second));

    edge[p0.first].push_back(make_pair(p1.first, cost));

    edge[p1.first].push_back(make_pair(p0.first, cost));

    p0 = p1;

  }

  cout << prim() << endl;

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.477022105120142x):
#include <bits/stdc++.h>



#define rep(i, n) for (int i = 0; i < int(n); i++)

#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)

#define reps(i, n) for (int i = 1; i <= int(n); i++)

#define rreps(i, n) for (int i = int(n); i >= 1; i--)

#define repc(i, n) for (int i = 0; i <= int(n); i++)

#define rrepc(i, n) for (int i = int(n); i >= 0; i--)

#define repi(i, a, b) for (int i = int(a); i < int(b); i++)

#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)

#define each(x, y) for (auto &x : y)

#define all(a) (a).begin(), (a).end()

#define bit(b) (1ll << (b))

#define uniq(v) (v).erase(unique(all(v)), (v).end())



using namespace std;



using i32 = int;

using i64 = long long;

using f80 = long double;

using vi32 = vector<i32>;

using vi64 = vector<i64>;

using vf80 = vector<f80>;

using vstr = vector<string>;



inline void yes() { cout << "Yes" << '\n'; exit(0); }

inline void no() { cout << "No" << '\n'; exit(0); }

inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); }

inline i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; }

void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; }

template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {};

template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {};

template <typename T> inline void amax(T &x, T y) { if (x < y) x = y; }

template <typename T> inline void amin(T &x, T y) { if (x > y) x = y; }

template <typename T> inline T power(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }

template <typename T> istream& operator>>(istream &is, vector<T> &v) { each(x, v) is >> x; return is; }

template <typename T> ostream& operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; }

template <typename T, typename S> istream& operator>>(istream &is, pair<T, S> &p) { is >> p.first >> p.second; return is; }

template <typename T, typename S> ostream& operator<<(ostream &os, pair<T, S> &p) { cout << p.first << ' ' << p.second; return os; }



struct UnionFind {

  vi32 par;

  vi32 siz;

  UnionFind(int n) {

    par = vi32(n);

    siz = vi32(n, 1);

    iota(all(par), 0);

  }

  int root(int x) {

    if (par[x] == x) return x;

    return par[x] = root(par[x]);

  }

  void unite(int x, int y) {

    x = root(x);

    y = root(y);

    if (x == y) return;

    par[x] = y;

    siz[y] += siz[x];

  }

  bool same(int x, int y) {

    return root(x) == root(y);

  }

  int size(int x) {

    return siz[root(x)];

  }

};



void solve() {

  int N; cin >> N;

  vi32 x(N), y(N);

  rep(i, N) cin >> x[i] >> y[i];



  pqasc<tuple<int, int, int>> pq;



  vi32 o(N);

  iota(all(o), 0);

  sort(all(o), [&](int i, int j) { return x[i] < x[j]; });

  rep(i, N - 1) {

    int c = abs(x[o[i + 1]] - x[o[i]]);

    pq.emplace(c, o[i], o[i + 1]);

  }

  sort(all(o), [&](int i, int j) { return y[i] < y[j]; });

  rep(i, N - 1) {

    int c = abs(y[o[i + 1]] - y[o[i]]);

    pq.emplace(c, o[i], o[i + 1]);

  }



  i64 ans = 0;



  auto uf = UnionFind(N);

  while (true) {

    int c, v, u;

    tie(c, v, u) = pq.top(), pq.pop();

    if (uf.same(v, u)) continue;

    uf.unite(v, u);

    ans += c;

    if (uf.size(v) == N) break;

  }



  cout << ans << '\n';

}