#include "bits/stdc++.h"

//region Template
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

const int MOD = 1000000007;

using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;

#define finish(x) cout << x; exit(0);
#define fastIO cin.tie(0); cin.sync_with_stdio(false);

void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }

//const string USCAO_TAG = "mountains";
void usaco(const string& USACO_TAG) { setIn(USACO_TAG + ".in"); setOut(USACO_TAG + ".out"); }
void print() {}
template<typename T, typename... Args>
void print(T firstArg, Args... args) {
	#ifdef LOCAL
	cout << firstArg << "\t";
	print(args...);
	#endif
}
//endregion

/* Notes:
 * Farah </3
 */

const int MAXN = 1e5 + 6;
const ll INF = 2e18;


pair<ll, ll> p[MAXN];
ll answer = 1e18;
int n;

void go(){
	vector<ll> x_y[MAXN];
	vector<ll> y_x[MAXN];

	set<ll> y;
	set<ll> x;
	sort(p, p + n);
	for(int i = 0; i < n; i++){
		cin >> p[i].first >> p[i].second;
		x_y[p[i].first].push_back(p[i].second);
		y_x[p[i].second].push_back(p[i].first);
	}
	ll ans = 0;
	for(auto x : x_y[p[0].first]){
		y.insert(x);
	}
	for(auto y : y_x[p[0].second]){
		x.insert(y);
	}



	for(int i = 1 ; i < n; i++){
		auto cur = p[i];
		auto l = y.lower_bound(p[i].second);
		auto lll = x.lower_bound(p[i].first);

		if(l == y.end()) l--;
		if(lll == x.end()) lll--;

		if(*l != p[i].second || *lll != p[i].first){
			ll m1 = abs(p[i].second - *l);
			if(l != y.begin()){
				l--;
				m1 = min(m1, abs(p[i].second - *l));
			}

			ll m2 = abs(p[i].first - *lll);
			if(lll != x.begin()){
				lll--;
				m2 = min(m2, abs(p[i].first - *lll));
			}
			ans += min(m1, m2);
		}

		if(y.count(p[i].second) == 0){
			for(auto x : x_y[p[i].first]){
				y.insert(x);
			}
		}

		if(x.count(p[i].first) == 0){
			for(auto y : y_x[p[i].second]){
				x.insert(y);
			}
		}

	}

	answer = min(ans, answer);
}
int main() {
	#ifdef LOCAL
	setIn("in.txt");
	setOut("out.txt");
	#endif

	cin >> n;

	go();
	for(int i = 0; i < n; i++){
		swap(p[i].first, p[i].second);
	}
	go();
	cout << answer;
}
