#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_map>
#include <vector>
#include <string.h>
#include <set>

using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> void printvec(const vector<T>& v) {
  for (auto x : v) { cout << x << " "; }
  cout << endl;
}
template<class T> void printtree(const vector< vector<T> >& tree) {
  for (long long i = 0; i < tree.size(); ++i) {
    cout << i + 1 << ": "; printvec(tree[i]);
  }
}
template<class T, class U> void printmap(const map<T, U>& mp) {
  for (auto x : mp) { cout << x.first << "=>" << x.second << endl; }
}

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

#define rep(i, n) for(ll i = 0; i < n; ++i)
#define all(s) s.begin(), s.end()
#define fr first
#define sc second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> triple;
typedef double D;

const ll INF = 1e9;
const ll MOD = 1000000007;  // 1e9 + 7

class UnionFind {
public:
  UnionFind(ll n) : par(n, -1), rnk(n, 0), cnt(n, 1), _size(n) {}

  bool same(ll x, ll y) {
    return root(x) == root(y);
  }
  void unite(ll x, ll y) {
    x = root(x); y = root(y);
    if (x == y) return;

    --_size;

    if (rnk[x] < rnk[y]) { swap(x, y); }
    par[y] = x;
    cnt[x] += cnt[y];
    if (rnk[x] == rnk[y]) { ++rnk[x]; }
  }
  ll root(ll x) {
    if (par[x] < 0) {
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }
  ll count(ll x) {
    return cnt[root(x)];
  }
  ll size() {
    return _size;
  }

private:
  vector<ll> par;
  vector<ll> rnk;
  vector<ll> cnt; // The number of vertices in each connected components.
  ll _size; // The number of connected components. Decreases by unite.
};

// int main(int argc, char** argv) {
//   ll N, M;
//   cin >> N >> M;
//   UnionFind tree(N);
//   rep(i, M) {
//     ll p, a, b;
//     cin >> p >> a >> b;
//     if (p == 0) { // Connect
//       tree.unite(a, b);
//     } else { // Judge
//       if (tree.same(a, b)) {
//         cout << "Yes" << endl;
//         cout << "size: " << tree.size() << endl;
//         cout << "count(" << a << "): " << tree.count(a) << endl;
//         cout << "count(" << b << "): " << tree.count(b) << endl;
//       } else {
//         cout << "No" << endl;
//         cout << "size: " << tree.size() << endl;
//         cout << "count(" << a << "): " << tree.count(a) << endl;
//         cout << "count(" << b << "): " << tree.count(b) << endl;
//       }
//     }
//   }
// }

int main(int argc, char** argv) {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  //cout << setprecision(10) << fixed;

  ll n;
  cin >> n;
  vector<P> xs; // pair of <x, id>
  vector<P> ys; // pair of <y, id>
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    xs.emplace_back(x, i);
    ys.emplace_back(y, i);
  }
  sort(all(xs));
  sort(all(ys));
  vector<triple> edges; // <cost, from, to>
  rep(i, n-1) {
    auto& from = xs[i];
    auto& to = xs[i+1];
    edges.emplace_back(abs(from.first - to.first), from.second, to.second);
  }
  rep(i, n-1) {
    auto& from = ys[i];
    auto& to = ys[i+1];
    edges.emplace_back(abs(from.first - to.first), from.second, to.second);
  }

  sort(all(edges));

  ll ans = 0;
  UnionFind uf(n);
  for (auto& edge : edges) {
    ll cost, from, to;
    tie(cost, from, to) = edge;
    if (uf.same(from, to)) { // already connected
      // Do nothing
    } else {
      uf.unite(from, to);
      ans += cost;
    }
  }
  cout << ans << endl;
}

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

#define int long long

struct edge {
    int to;
    int cost;
};

using namespace std;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
using graph = vector<vector<edge>>;
// using graph = vector<vector<int>>;


#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)
#define delim(i, n) cout << (i == n-1 ? "\n" : " ")
#define print1(v) cout << fixed << setprecision(10) << v << "\n"
#define print2(v1, v2) cout << fixed << setprecision(10) << v1 << " " << v2 << "\n"
#define printvi(v, n) REP(iiii, n) { cout << v[iiii]; delim(iiii, n); }
#define printvii(v, h, w) REP(iiiii, h) { printvi(v[iiiii], w); }

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;

const int maxn = 1e5;
int n;
vi x(maxn);
vi y(maxn);
vb used(maxn);
graph G(maxn);

vi dx = {-1, 0, 0, 1};
vi dy = {0, -1, 1, 0};

// O(|E|log|V|)
int prim() {
    int s = 0;
    used.assign(n, false);
    priority_queue<P, vector<P>, greater<P>> pq;
    pq.emplace(0, s);
    int ret = 0;
    while (!pq.empty()) {
        P p = pq.top();
        pq.pop();
        int v = p.second;
        if (used[v]) continue;
        used[v] = true;
        ret += p.first;
        for (auto &e : G[v]) pq.emplace(e.cost, e.to);
    }
    return ret;
}

signed main () {
	cin.tie(0);
   	ios::sync_with_stdio(false);

    cin >> n;
    REP(i, n) {
        cin >> x[i] >> y[i];
    }

    vi idx(n);
    vi idy(n);
    REP(i, n) {
        idx[i] = i;
        idy[i] = i;
    }
    sort(idx.begin(), idx.end(), [&](int i, int j) { return x[i] < x[j]; });
    sort(idy.begin(), idy.end(), [&](int i, int j) { return y[i] < y[j]; });

    REP(i, n-1) {
        edge e;
        int s = idx[i];
        e.to = idx[i+1];
        e.cost = x[e.to] - x[s];
        G[s].push_back(e);
        swap(e.to, s);
        G[s].push_back(e);
    }
    REP(i, n-1) {
        edge e;
        int s = idy[i];
        e.to = idy[i+1];
        e.cost = y[e.to] - y[s];
        G[s].push_back(e);
        swap(e.to, s);
        G[s].push_back(e);
    }
    
    int ret = prim();

    print1(ret);
}