/*
これを入れて実行
g++ code.cpp
./a.out
 */
 
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;
 
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}
 
ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}
 
ll nCr(ll n, ll r){
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
 
ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
 
//-----------------------ここから-----------
struct Edge{
    ll from;
    ll to;
    ll cost;
};
ll prim(vector<vector<Edge>> &g, ll n){
    ll res = 0;
    vector<int> used(n, 0);
    priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> q;
    q.push(make_pair(0, 0));
    while(!q.empty()){
        pair<ll,ll> now = q.top();
        q.pop();
        if(used[now.second]) continue;
        used[now.second] = 1;
        res += now.first;
        for(int i = 0; i < g[now.second].size(); i++){
            Edge next = g[now.second][i];
            if(!used[next.to]){
                q.push(make_pair(next.cost, next.to));
            }
        }
    }
    return res;
}

int main(void){
    ll n;
    cin >> n;
    vector<vector<Edge>> g(n);
    vector<pair<pair<ll,ll>, ll>> place(n);
    vector<pair<pair<ll,ll>, ll>> placey(n);
    for(int i = 0; i < n; i++){
        ll x, y;
        cin >> x >> y;
        x--;
        y--;
        place[i] = make_pair(make_pair(x, y), i);
        placey[i] = make_pair(make_pair(y, x), i);
    }

    sort(place.begin(), place.end());

    for(int i = 1; i < n; i++){
        ll lx, ly, li;
        lx = place[i - 1].first.first;
        ly = place[i - 1].first.second;
        li = place[i - 1].second;
        ll nx, ny, ni;
        nx = place[i].first.first;
        ny = place[i].first.second;
        ni = place[i].second;
        ll costx = abs(lx - nx);
        ll costy = abs(ly - ny);

        Edge tmp;
        tmp.cost = costx;
        tmp.to = ni;
        g[li].push_back(tmp);
        tmp.to = li;
        g[ni].push_back(tmp);
        tmp.cost = costy;
        tmp.to = ni;
        g[li].push_back(tmp);
        tmp.to = li;
        g[ni].push_back(tmp);
    }

    sort(placey.begin(), placey.end());

    for(int i = 1; i < n; i++){
        ll lx, ly, li;
        ly = placey[i - 1].first.first;
        lx = placey[i - 1].first.second;
        li = placey[i - 1].second;
        ll nx, ny, ni;
        ny = placey[i].first.first;
        nx = placey[i].first.second;
        ni = placey[i].second;
        ll costx = abs(lx - nx);
        ll costy = abs(ly - ny);

        Edge tmp;
        tmp.cost = costx;
        tmp.to = ni;
        g[li].push_back(tmp);
        tmp.to = li;
        g[ni].push_back(tmp);
        tmp.cost = costy;
        tmp.to = ni;
        g[li].push_back(tmp);
        tmp.to = li;
        g[ni].push_back(tmp);
    }
    cout << prim(g, n) << endl;
}

Measured memory_bytes: 40172