// {{{
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
 
using namespace std;
 
typedef long int i32;
typedef unsigned long int u32;
typedef long long int i64;
typedef unsigned long long int  u64;
typedef pair<i32, i32> PII;
typedef vector<i32> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<i32, i32> MPII;
typedef set<i32> SETI;
typedef multiset<i32> MSETI;
#define IN(x) cin >> x;
#define SCD(t) scanf("%d",&t)
#define SCLD(t) scanf("%ld",&t)
#define SCLLD(t) scanf("%lld",&t)
#define SCC(t) scanf("%c",&t)
#define SCS(t) scanf("%s",t)
#define SCF(t) scanf("%f",&t)
#define SCLF(t) scanf("%lf",&t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define ZERO(a) memset(a, 0, sizeof(a))
#define FOR(i, j, k, in) for (int i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for (int i=j ; i>=k ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define BTW(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define PF push_front
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define DEBUG(x) cout << #x << ": " << x << endl;
 
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
// }}}
 
const u32 MAX_N = 100000;
i32 x[MAX_N], y[MAX_N];
vector<pair<i32, i32>> xs;
vector<pair<i32, i32>> ys;
vector<pair<i32, i32>> e[MAX_N];
bool used[MAX_N];
 
inline u32 calc(i32 x1, i32 y1, i32 x2, i32 y2) {
    return min(abs(x1-x2), abs(y1-y2));
}
 
int main() {
    u32 N;
    IN(N);
    REP(i, N) {
        cin >> x[i] >> y[i];
        xs.PB(MP(x[i], i));
        ys.PB(MP(y[i], i));
    }
    sort(ys.begin(), ys.end());
    sort(xs.begin(), xs.end());
    auto q = priority_queue<pair<u32, u32>, vector<pair<u32, u32>>,
         greater<pair<u32, u32>>>();
    used[0] = true;
    u32 ans = 0;
    REP(i, N-1) {
        i32 x1 = xs[i].second;
        i32 x2 = xs[i+1].second;
        u32 cost1 = calc(x[x1], y[x1], x[x2], y[x2]);
        e[x1].PB(MP(cost1, x2));
        e[x2].PB(MP(cost1, x1));
 
        i32 y1 = ys[i].second;
        i32 y2 = ys[i+1].second;
        u32 cost2 = calc(x[y1], y[y1], x[y2], y[y2]);
        e[y1].PB(MP(cost2, y2));
        e[y2].PB(MP(cost2, y1));
    }
    REP(i, e[0].size()) {
        q.push(e[0][i]);
    }
    while(!q.empty()) {
        auto vi = q.top();
        q.pop();
        auto v = vi.first;
        auto index = vi.second;
        if(used[index]) {
            continue;
        }
        ans += v;
        used[index] = true;
 
        for(auto a : e[index]) {
            if(used[a.second]) {
                continue;
            }
            q.push(a);
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 19044