Problem:
Score : 500 points

Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

3
1 5
3 9
7 8

Sample Output 1

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

8

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
long long int x[100005], y[100005];
int asc1(const void* a, const void* b)
{
	if (x[*(long long int*)a] > x[*(long long int*)b])return 1;
	if (x[*(long long int*)a] < x[*(long long int*)b])return -1;
	return 0;
}
int asc2(const void* a, const void* b)
{
	if (y[*(long long int*)a] > y[*(long long int*)b])return 1;
	if (y[*(long long int*)a] < y[*(long long int*)b])return -1;
	return 0;
}
long long int llabs(long long int n)
{
	if (n < 0)
		n *= -1;
	return n;
}
long long int dist(long long int a, long long int b)
{
	if (llabs(x[a] - x[b]) < llabs(y[a] - y[b]))
		return llabs(x[a] - x[b]);
	else
		return llabs(y[a] - y[b]);
}
long long int h[200005], l;
void swap_h(long long int a, long long  int b)
{
	long long int f = h[a];
	h[a] = h[b];
	h[b] = f;
	return;
}
int comp_h(long long int a, long long int b)
{
	long long int a1, a2, b1, b2;
	a = h[a];
	b = h[b];
	a1 = a / 100000;
	a2 = a % 100000;
	b1 = b / 100000;
	b2 = b % 100000;
	if (dist(a1, a2) > dist(b1, b2))return 1;
	return 0;
}
void f1(long long int n)
{
	h[l] = n;
	long long int now = l;
	l++;
	while (now > 0)
	{
		if (comp_h((now - 1) / 2, now) > 0)
			swap_h((now - 1) / 2, now);
		now = (now - 1) / 2;
	}
	return;
}
long long int f2()
{
	l--;
	swap_h(0, l);
	long long int now = 0;
	for (;;)
	{
		if (2 * now + 2 < l)
		{
			if (comp_h(2 * now + 1, 2 * now + 2) > 0)
			{
				if (comp_h(now, 2 * now + 2) > 0)
					swap_h(now, 2 * now + 2);
				now = 2 * now + 2;
			}
			else
			{
				if (comp_h(now, 2 * now + 1) > 0)
					swap_h(now, 2 * now + 1);
				now = 2 * now + 1;
			}
		}
		else if (2 * now + 1 < l)
		{
			if (comp_h(now, 2 * now + 1) > 0)
				swap_h(now, 2 * now + 1);
			now = 2 * now + 1;
		}
		else
			break;
	}
	return h[l];
}
long long int p[100005];
long long int root(long long int n)
{
	if (p[n] != n)
		p[n] = root(p[n]);
	return p[n];
}
void uni(long long int a, long long int b)
{
	a = root(a);
	b = root(b);
	if (a != b)
		p[a] = b;
	return;
}
int main()
{
	long long int n;
	scanf("%lld", &n);
	long long int i, j;
	for (i = 0; i < n; i++)
		scanf("%lld %lld", &x[i], &y[i]);
	long long int a[100005], b[100005];
	for (i = 0; i < n; i++)
		a[i] = b[i] = i;
	qsort(a, n, sizeof(long long int), asc1);
	qsort(b, n, sizeof(long long int), asc2);
	l = 0;
	for (i = 0; i < n - 1; i++)
		f1(100000 * a[i] + a[i + 1]);
	for (i = 0; i < n - 1; i++)
		f1(100000 * b[i] + b[i + 1]);
	for (i = 0; i < n; i++)
		p[i] = i;
	long long int ans = 0;
	while (l > 0)
	{
		i = f2();
		j = i / 100000;
		i %= 100000;
		if (root(i) != root(j))
		{
			ans += dist(i, j);
			uni(i, j);
		}
	}
	printf("%lld\n", ans);
	return 0;
}