//#include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include<cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include<iomanip>
#include<unordered_set>
#include<bitset>
#include<complex>

using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

//typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;

template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

struct UnionFind
{
	vector<int> parent;
	vector<int> rank;

	UnionFind(int N) :parent(N, 0), rank(N, 0)
	{
		rep(i, 0, N)
		{
			parent[i] = i;
		}
	}

	int find(int x)
	{
		if (parent[x] == x)
		{
			return x;
		}
		else
		{
			return parent[x] = find(parent[x]);
		}
	}

	void unite(int x, int y)
	{
		x = find(x);
		y = find(y);

		if (x == y)
		{
			return;
		}

		if (rank[x] < rank[y])
		{
			parent[x] = y;
		}
		else
		{
			parent[y] = x;
			if (rank[x] == rank[y])
			{
				rank[x]++;
			}
		}
	}

	bool same(int x, int y)
	{
		return find(x) == find(y);
	}
};

struct Edge
{
	int s, t, cost;
	Edge(int s, int t, int cost) :s(s), t(t), cost(cost) {}
	Edge() :Edge(-1, -1, -1) {}
	bool operator<(const Edge& e)const
	{
		return cost < e.cost;
	}
};

int N;
int OX[100010],OY[100010];
pii X[100010], Y[100010];

vector<Edge> edges;

int minimum_spaning_tree()
{
	sort(all(edges));
	UnionFind uf(N);

	int ans = 0;
	for (auto edge : edges)
	{
		if (!uf.same(edge.s, edge.t))
		{
			ans += edge.cost;
			uf.unite(edge.s, edge.t);
		}
	}

	return ans;
}

signed main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N;
	rep(i, 0, N)
	{
		cin >> OX[i] >> OY[i];
		X[i].first = OX[i];
		Y[i].first = OY[i];
		X[i].second = Y[i].second = i;
	}

	sort(X, X + N);
	sort(Y, Y + N);

	rep(i, 0, N)
	{
		int idx = lower_bound(X, X + N, mp(OX[i], i)) - X;
		if (idx > 0)
		{
			edges.push_back(Edge(i, X[idx - 1].second, abs(X[idx - 1].first - OX[i])));
		}
		if (idx < N - 1)
		{
			edges.push_back(Edge(i, X[idx + 1].second, abs(X[idx + 1].first - OX[i])));
		}

		idx = lower_bound(Y, Y + N, mp(OY[i], i)) - Y;
		if (idx > 0)
		{
			edges.push_back(Edge(i, Y[idx - 1].second, abs(Y[idx - 1].first - OY[i])));
		}
		if (idx < N - 1)
		{
			edges.push_back(Edge(i, Y[idx + 1].second, abs(Y[idx + 1].first - OY[i])));
		}
	}

	int ans = minimum_spaning_tree();

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 19436