#include<bits/stdc++.h>
#define int long long
#define out(x) cerr << #x << " = " << x << "\n"
using namespace std;
// by piano
template<typename tp> inline void read(tp &x) {
	x = 0;char c = getchar();bool f = 0;
	for(; c < '0' || c > '9'; f |= (c == '-'), c = getchar());
	for(; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
	if(f) x = -x;
}
#define P pair<int, int>
#define F first
#define S second
const int N = 1e6 + 10 ;
set<P> s, xx;
P p[N];
int n, ans = 0 ,now = 0;
main() {
	read(n);	
	for(int i = 1; i <= n; i ++) {
		read(p[i].F), read(p[i].S);
		s.insert(make_pair(p[i].S, i));
		xx.insert(make_pair(p[i].F, i));
	}
//	sort(p + 1, p + n + 1);
//	s.insert(make_pair(p[1].S, 1));
//	xx.insert(make_pair(p[1].F, 1));
	for(int i = 1; i <= n - 1; i ++) {
		int deltax = abs(p[i].F - p[i - 1].F), now = 1234567891011LL;
		set<P> ::iterator it = s.lower_bound(make_pair(p[i].S, 0));
		if(++ it != s.end()) now = min(now, abs(it -> F - p[i].S)); -- it;
		if(it != s.begin()) now = min(now, abs((--it) -> F - p[i].S));
//		s.erase(make_pair(p[i].S, i));
		 it = xx.lower_bound(make_pair(p[i].F, 0));
		if(++ it != xx.end())now = min(now, abs(it -> F - p[i].F)); -- it;
		if(it != xx.begin()) now = min(now, abs((--it) -> F - p[i].F));
//		xx.insert(make_pair(p[i].F, i));
		ans += now;
	}
	cout << ans << "\n";
	return 0;
}