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

using namespace std;

#define ll long long

struct p {

	int x, y;

	int indx;

};

const int N = 1e5 + 5;

p points[N];

int n;

int par[N];

int get(int n) {

	if (par[n] == n) return n;

	return par[n] = get(par[n]);

}

vector<int> g[N];

void solve() {

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

		par[i] = i;

	vector<pair<int, pair<int, int> >> edg;

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

//		g[i].clear();

//		g[i].resize(n);

		for (int j = 0; j < g[i].size(); j++) {

//			g[i][j] = j;

			int v = g[i][j];

			ll c = min(abs(points[i].x - points[v].x),

					abs(points[i].y - points[v].y));

			edg.push_back(make_pair(c, make_pair(i, v)));

		}

	}

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

	ll ans = 0;

	for (int i = 0; i < edg.size(); i++) {

		int u = edg[i].second.first;

		int v = edg[i].second.second;

		int r1 = get(u);

		int r2 = get(v);

		if (r1 == r2) continue;

		//cout << u << "  " << v << endl;

		ans += edg[i].first;

		par[r1] = r2;

	}

	cout << ans << endl;

}

void gen(vector<pair<int, int> > &a, vector<pair<int, int> > &b) {



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



	for (int i = 0; i < a.size(); i++) {



		auto it = lower_bound(b.begin(), b.end(), make_pair(a[i].first, -1));

		if (it != b.end()) {

			g[a[i].second].push_back(it->second);

			g[it->second].push_back(a[i].second);

		}

		if (it != b.begin()) {

			it--;



			g[a[i].second].push_back(it->second);

			g[it->second].push_back(a[i].second);

		}

	}

}

int main() {

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cin >> n;

	vector<pair<int, pair<int, int> > > v;

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

		cin >> points[i].x >> points[i].y;

		points[i].indx = i;

		v.push_back(make_pair(points[i].x, make_pair(points[i].y, i)));

	}

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

	vector<pair<int, int> > last, cur;

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

		int X = v[i].first;

		while (i < n && v[i].first == X) {

			cur.push_back(v[i].second);

			i++;

		}

		if (last.size()) gen(last, cur);

		last = cur;

		cur.clear();

	}

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

		int u = v[i].second.second;

		int V = v[i + 1].second.second;

		g[u].push_back(V);

		g[V].push_back(u);

	}

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

		swap(v[i].first, v[i].second.first);

	last.clear();

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

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

		int X = v[i].first;



		while (i < n && v[i].first == X) {

			cur.push_back(v[i].second);

			i++;

		}

		if (last.size()) gen(last, cur);

		last = cur;

		cur.clear();

	}

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

		int u = v[i].second.second;

		int V = v[i + 1].second.second;

		g[u].push_back(V);

		g[V].push_back(u);

	}



	solve();

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.915231486533704x):
#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;

}