#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

#define MOD 1000000007
#define int long long
//#define PI 3.14159265358979

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

template < typename T >
ostream &operator<<(ostream &os, const vector< T > &A) {
	for (int i = 0; i < A.size(); i++)
		os << A[i] << " ";
	os << endl;
	return os;
}
template <>
ostream &operator<<(ostream &os, const vector< vector< int > > &A) {
	int N = A.size();
	int M;
	if (N > 0)
		M = A[0].size();
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++)
			os << A[i][j] << " ";
		os << endl;
	}
	return os;
}

typedef pair< int, int > pii;
typedef long long ll;

struct edge {
	int from, to, d, c;
	edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) {
		from = _from;
		to = _to;
		d = _d;
		c = _c;
	}
	bool operator<(const edge &rhs) const {
		return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d);
	}
};

typedef vector< edge > edges;
typedef vector< edges > graph;
struct flow {
	int to, cap, rev, cost;
	flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {}
};
typedef vector< vector< flow > > flows;

const int di[4] = {0, -1, 0, 1};
const int dj[4] = {-1, 0, 1, 0};
const int ci[5] = {0, 0, -1, 0, 1};
const int cj[5] = {0, -1, 0, 1, 0};
const ll LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const double PI = acos(-1);

template < typename T, typename U >
bool chmin(T &x, const U &y) {
	if (x > y) {
		x = y;
		return true;
	}
	return false;
}
template < typename T, typename U >
bool chmax(T &x, const U &y) {
	if (x < y) {
		x = y;
		return true;
	}
	return false;
}

struct initializer {
	initializer() {
		cout << fixed << setprecision(11);
	}
};
initializer _____;
struct UFT {
	vector< int > par;
	int getpar(int i) {
		if (par[i] == i)
			return i;
		return par[i] = getpar(par[i]);
	}
	void unite(int i, int j) {
		int t = getpar(i);
		par[t] = getpar(j);
	}
	bool uf(int i, int j) {
		return getpar(i) == getpar(j);
	}
	UFT(int n) {
		par = vector< int >(n);
		for (int i = 0; i < n; i++)
			par[i] = i;
	}
};
int minT(edges &es, int n) {
	sort(es.begin(), es.end());
	int ret = 0;
	UFT *u = new UFT(n);
	for (int i = 0; i < es.size(); i++) {
		edge e = es[i];
		if (u->uf(e.from, e.to))
			continue;
		ret += e.d;
		u->unite(e.from, e.to);
	}
	for (int i = 1; i < n; i++)
		if (!u->uf(0, i))
			return -1;
	return ret;
}
struct range {
	int l, r, c;
	range(int l = 0, int r = 0, int c = 0) : l(l), r(r), c(c) {}
	bool operator<(const range &rhs) const {
		return l < rhs.l;
	}
};
int N, M, K, T, Q;
signed main() {
	cin >> N;
	vector< range > A(N), B(N);
	rep(i, N) {
		cin >> A[i].l >> A[i].r;
		A[i].c = i;
	}
	rep(i, N) {
		B[i].l = A[i].r;
		B[i].r = A[i].l;
		B[i].c = A[i].c;
	}
	sort(A.begin(), A.end());
	sort(B.begin(), B.end());
	edges es;
	rep(i, N - 1) {
		es.push_back(edge(A[i].c, A[i + 1].c, abs(A[i].l - A[i + 1].l)));
		es.push_back(edge(B[i].c, B[i + 1].c, abs(B[i].l - B[i + 1].l)));
	}
	int ans = minT(es, N);
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 15212