// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



#define fi first

#define se second

const int inf = 1e9 + 7;

int n, par[100010], ans = 0;

pair <pair <int, int>, int> a[100010]; bool dis[100010];

priority_queue<pair <int, pair <int, int> >, vector<pair <int, pair <int, int> > >, greater<pair <int, pair <int, int> > > > wl1, wl2;

int find(int a) {

	return (par[a] < 0 ? a : par[a] = find(par[a]));

}

void unionset(int a, int b) {

	int c = find(a), d = find(b);

	if(par[c] < par[d]) par[c] += par[d], par[d] = c;

	else par[d] += par[c], par[c] = d;

}

int main() {

	memset(par, -1, sizeof(par));

	scanf("%d", &n);

	for(int i = 0; i < n; i++) {

		scanf("%d%d", &a[i].fi.fi, &a[i].fi.se);

		a[i].se = i;

	}

	sort(a, a + n);

	for(int i = 0; i < n-1; i++) {

		wl1.push({a[i+1].fi.fi - a[i].fi.fi, {a[i].se, a[i+1].se}});

		swap(a[i].fi.fi, a[i].fi.se);

	}

	swap(a[n-1].fi.fi, a[n-1].fi.se);

	sort(a, a + n);

	for(int i = 0; i < n-1; i++) wl2.push({a[i+1].fi.fi - a[i].fi.fi, {a[i].se, a[i+1].se}});

	for(int i = 1; i < n; i++) {

		while(find(wl1.top().se.fi) == find(wl1.top().se.se)) wl1.pop();

		while(find(wl2.top().se.fi) == find(wl2.top().se.se)) wl2.pop();

		int d1 = inf, d2 = inf, u1, u2, v1, v2;

		if(not wl1.empty()) d1 = wl1.top().fi, u1 = wl1.top().se.fi, v1 = wl1.top().se.se;

		if(not wl2.empty()) d2 = wl2.top().fi, u2 = wl2.top().se.fi, v2 = wl2.top().se.se;

		if(d1 < d2) {

			unionset(u1, v1);

			ans += d1;

		}

		else {

			unionset(u2, v2);

			ans += d2;

		}

	}

	printf("%d\n", ans);

}

// Faster C++ solution for the same problem (measured speedup: 1.3759785027837654x):
#define _USE_MATH_DEFIMES

#include <algorithm>

#include <array>

#include <bitset>

#include <cassert>

#include <cctype>

#include <climits>

#include <clocale>

#include <cmath>

#include <complex>

#include <cstdio>

#include <cstdlib>

#include <ctime>

#include <deque>

#include <fstream>

#include <functional>

#include <iomanip>

#include <iostream>

#include <iterator>

#include <list>

#include <map>

#include <numeric>

#include <queue>

#include <regex>

#include <set>

#include <sstream>

#include <stack>

#include <string>

#include <tuple>

#include <unordered_map>

#include <unordered_set>

#include <utility>

#include <vector>



const int MOD = 1'000'000'007;

const int INF = 1'000'000'000; //1e9

const int NIL = -1;

const long long LINF = 1'000'000'000'000'000'000; // 1e18

const long double EPS = 1E-10;



template<class T, class S> inline bool chmax(T &a, const S &b){

    if(a < b){

        a = b; return true;

    }

    return false;

}

template<class T, class S> inline bool chmin(T &a, const S &b){

    if(b < a){

        a = b; return true;

    }

    return false;

}





struct edge{

    int to, cost;

    edge(int To, int Cost): to(To), cost(Cost){};

};





long long prim(std::vector<std::vector<edge>>& G){

    int V(G.size());

    long long res(0);

    //std::pair<int, int> first: 距離　second: 頂点

    std::priority_queue<std::pair<int, int>,

                        std::vector<std::pair<int, int>>,

                        std::greater<std::pair<int, int>>> que;

    std::vector<int> d(V, INF);

    d[0] = 0;

    que.push({0, 0});



    while(!que.empty()){

        std::pair<int, int> p = que.top(); que.pop();

        int v(p.second);

        if(d[v] < p.first) continue;

        d[v] = 0;

        res += p.first;

        for(edge &e : G[v]){

            if(d[e.to] > e.cost){

                d[e.to] = e.cost;

                que.push({d[e.to], e.to});

            }

        }

    }

    return res;

}



int main(){

    int N; std::cin >> N;

    std::vector<std::vector<edge>> G(N);

    //x,y,id と y,x,id

    std::vector<std::pair<std::pair<int, int>, int>> px(N), py(N);

    for(int i(0); i < N; ++i){

        px[i].second = py[i].second = i;

        std::cin >> px[i].first.first >> px[i].first.second;

        py[i].first.first = px[i].first.second;

        py[i].first.second = px[i].first.first;

    }

    std::sort(std::begin(px), std::end(px));

    std::sort(std::begin(py), std::end(py));

    for(int i(0), i_len(N-1); i < i_len; ++i){

        G[px[i].second].emplace_back(px[i+1].second, 

                                     std::abs(px[i].first.first - px[i+1].first.first));

        G[px[i+1].second].emplace_back(px[i].second, 

                                     std::abs(px[i].first.first - px[i+1].first.first));

        G[py[i].second].emplace_back(py[i+1].second, 

                                     std::abs(py[i].first.first - py[i+1].first.first));

        G[py[i+1].second].emplace_back(py[i].second, 

                                     std::abs(py[i].first.first - py[i+1].first.first));

    }

    std::cout << prim(G) << std::endl;

    return 0;

}