#include #include #include #include

using namespace std;

struct point { int x, y; void read() { scanf("%d%d", &x, &y); } }; struct edge { int t, v; edge *ne; };

typedef pair <int, int> dpr;

const int maxn = 200003;

int n, xo[maxn], yo[maxn], d[maxn]; point p[maxn]; edge ebuf_arr[maxn « 2], *ebuf(ebuf_arr), *head[maxn];

inline bool xCmp(const int& a, const int& b) { return p[a]. x < p[b]. x; } inline bool yCmp(const int& a, const int& b) { return p[a]. y < p[b]. y; }

inline int dist(const point& a, const point& b) { return min(abs(a. x - b. x), abs(a. y - b. y)); }

inline void addEdge(int u, int v, int w) { ebuf-> t = v; ebuf-> v = w; ebuf-> ne = head[u]; head[u] = ebuf ++; }

int dijkstra() { static dpr hp[maxn « 2]; int th(1); hp[0] = dpr(0, 1); memset(d, -1, sizeof(d)); while (th && d[n] == -1) { dpr g(hp[0]); pop_heap(hp, hp + th –); if (d[g. second] > -1) continue; d[g. second] = -g. first; for (edge* e = head[g. second]; e; e = e-> ne) if (d[e-> t] == -1) { hp[th] = dpr(g. first - e-> v, e-> t); push_heap(hp, hp + ++ th); } } return d[n]; }

int main() { #ifdef LAEKOV_LOCAL freopen(".in", “r”, stdin); #endif

scanf("%d", &n);
for (int i = 1; i <= n; ++ i) {
	p[i]. read();
	xo[i - 1] = yo[i - 1] = i;
}
sort(xo, xo + n, xCmp);
sort(yo, yo + n, yCmp);
for (int i = 0; i + 1 < n; ++ i) {
	addEdge(xo[i], xo[i + 1], dist(p[xo[i]], p[xo[i + 1]]));
	addEdge(xo[i + 1], xo[i], dist(p[xo[i]], p[xo[i + 1]]));
	addEdge(yo[i], yo[i + 1], dist(p[yo[i]], p[yo[i + 1]]));
	addEdge(yo[i + 1], yo[i], dist(p[yo[i]], p[yo[i + 1]]));
}
printf("%d\n", dijkstra());

}