#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;

// Graph Templates
using Weight = ll;
// using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    // Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
const Weight INF = numeric_limits<Weight>::max() / 8;

// add edge a <--(weight = w)--> b
void add_edge(Graph &g, int a, int b, Weight w) {
    g[a].push_back(Edge(a, b, w));
    g[b].push_back(Edge(b, a, w));
}

// add edge a --(weight = w)--> b
void add_arc(Graph &g, int a, int b, Weight w) { g[a].push_back(Edge(a, b, w)); }

struct uf_tree {
    std::vector<int> parent;
    int __size;
    uf_tree(int size_) : parent(size_, -1), __size(size_) {}
    void unite(int x, int y) {
        if ((x = find(x)) != (y = find(y))) {
            if (parent[y] < parent[x]) std::swap(x, y);
            parent[x] += parent[y];
            parent[y] = x;
            __size--;
        }
    }
    bool is_same(int x, int y) { return find(x) == find(y); }
    int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
    int size(int x) { return -parent[find(x)]; }
    int size() { return __size; }
};

// Kruskal
pair<Weight, Edges> kruskal(Graph const &g) {
    uf_tree uf(g.size());
    Edges es;
    for (auto &adj : g)
        for (auto &e : adj) es.emplace_back(e);
    sort(es.begin(), es.end(), [](const Edge &e, const Edge &f) { return e.weight < f.weight; });
    Weight total = 0;
    Edges T;
    for (auto &e : es)
        if (!uf.is_same(e.src, e.dst)) {
            T.push_back(e);
            total += e.weight;
            uf.unite(e.src, e.dst);
        }
    return make_pair(total, T);
}

struct pos {
    ll x;
    ll y;
};

bool pair_sort_y (pair<ll, ll> p1, pair<ll, ll> p2){
    if(p1.second != p2.second){
        return p1.second > p2.second;
    }
    else {
        return p1.first > p1.first;
    }
}

int main(){
    int n;
    cin >> n;
    Graph g;
    g.resize(n);
    vector<pair<ll, ll>> pos(n);
    ll x, y, dist;
    map<pair<ll, ll>, ll> vertex; // (x, y) -> vertex number (0 ~ n-1)
    for (int i=0; i<n; ++i){
        cin >> x >> y;
        pos[i] = make_pair(x, y);
        vertex.insert(make_pair(pos[i], i));
    }
    sort(pos.begin(), pos.end());
    for (int i=0; i<n-1; ++i){
        dist = min(abs(pos[i].first - pos[i+1].first), abs(pos[i].second - pos[i+1].second));
        ll v1, v2;
        v1 = vertex.at(pos[i]);
        v2 = vertex.at(pos[i+1]);
        // cout << i << " -> (" << v1 << ", " << v2 << ")" << endl;
        add_edge(g, v1, v2, dist);
    }
    sort(pos.begin(), pos.end(), pair_sort_y);
    for (int i=0; i<n-1; ++i){
        dist = min(abs(pos[i].first - pos[i+1].first), abs(pos[i].second - pos[i+1].second));
        ll v1, v2;
        v1 = vertex.at(pos[i]);
        v2 = vertex.at(pos[i+1]);
        // cout << i << " -> (" << v1 << ", " << v2 << ")" << endl;
        add_edge(g, v1, v2, dist);
    }
    pair<Weight, Edges> ans = kruskal(g);
    cout << ans.first << endl;
    return 0;
}

Measured memory_bytes: 31468