#include <iostream>
#include <iomanip> //! for setprecision(10)
#include <math.h>
#include <algorithm>
#include <functional> 
#include <string>
#include <vector>

#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <utility>

#include <algorithm>
#include <cassert>
#include <vector>

namespace atcoder {

// Implement (union by size) + (path compression)
// Reference:
// Zvi Galil and Giuseppe F. Italiano,
// Data structures and algorithms for disjoint set union problems
struct dsu {
  public:
    dsu() : _n(0) {}
    dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

}  // namespace atcoder


using namespace std;
using namespace atcoder;

typedef long long LL;

#define dump(c) { for (auto it = c.begin(); it != c.end(); ++it) if (it == c.begin()) cout << *it; else cout << ' ' << *it; cout << endl; } 
#define rep(i,n) for (int i = 0; i < (n); ++i)

const int MOD = 1000000007;
const int INF = 1e9;
const LL INFL = 1e18;


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

    int n;
    cin >> n;
    vector<pair<int, int>> xi(n), yi(n);
    rep(i, n) {
        cin >> xi[i].first >> yi[i].first;
        xi[i].second = i;
        yi[i].second = i;
    }
    sort(xi.begin(), xi.end());
    sort(yi.begin(), yi.end());

    vector<tuple<int, int, int>> ed;
    rep(i, n-1) {
        ed.emplace_back(abs(xi[i].first-xi[i+1].first), xi[i].second, xi[i+1].second);
        ed.emplace_back(abs(yi[i].first-yi[i+1].first), yi[i].second, yi[i+1].second);
    }
    sort(ed.begin(), ed.end());

    dsu d(n);
    int ans = 0;
    int ind = 0;
    rep(i, n-1) {
        int added = false;
        while (!added) {
            int cost = get<0>(ed[ind]);
            int s = get<1>(ed[ind]);
            int t = get<2>(ed[ind]);
            ++ind;
            if (!d.same(s, t)) {
                d.merge(s, t);
                ans += cost;
                added = true;
            }
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 7884