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 Java7 (OpenJDK 1.7.0) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;

class Main{
	public static void main(String[] args) throws Exception{
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(reader.readLine());
		Map<Integer,HashSet<Integer>> tate = new TreeMap<>();
		Map<Integer,HashSet<Integer>> yoko = new TreeMap<>();
		for(int i = 0; i < n; i++) {
			String[] _input = reader.readLine().split(" ");
			int _x = Integer.parseInt(_input[0]);
			int _y = Integer.parseInt(_input[1]);
			if(!tate.containsKey(_x)) {
				HashSet<Integer> _hs = new HashSet<>();
				_hs.add(i);
				tate.put(_x,_hs);
			}else {
				tate.get(_x).add(i);
			}
			if(!yoko.containsKey(_y)) {
				HashSet<Integer> _hs = new HashSet<>();
				_hs.add(i);
				yoko.put(_y,_hs);
			}else {
				yoko.get(_y).add(i);
			}
		}
/*
				for(Map.Entry e : tate.entrySet()) {
					System.out.println(e.getKey()+" , "+e.getValue());
				}
						for(Map.Entry e : yoko.entrySet()) {
							System.out.println(e.getKey()+" , "+e.getValue());
						}
*/

		PriorityQueue<MySet> pq = new PriorityQueue<>();

		int _pre = -1;
		for(Map.Entry e : tate.entrySet()) {
			int key = (int)e.getKey();
			if(_pre==-1) {
				_pre = key;
			}else {
				pq.add(new MySet(true,key,key-_pre));
				_pre = key;
			}
		}
		_pre = -1;
		for(Map.Entry e : yoko.entrySet()) {
			int key = (int)e.getKey();
			if(_pre==-1) {
				_pre = key;
			}else {
				pq.add(new MySet(false,key,key-_pre));
				_pre = key;
			}
		}

		int[] checked = new int[n];
		for(int i = 0; i < n; i++) checked[i] = i;

		long result = 0;
		while(!pq.isEmpty()) {
			//System.out.println(Arrays.toString(checked));
			MySet ms = pq.poll();
			Map<Integer,HashSet<Integer>> map = null;
			if(ms.isTate) {
				map = tate;
			}else {
				map = yoko;
			}
			//System.out.println(ms.isTate+","+ms.index+","+ms.value);
			HashSet<Integer> hs = map.get(ms.index);
			int checked_town1 = -1;
			int checked_town2 = -1;
			int group_num = -1;
			int town1 = -1;
			int town2 = -1;
			for(int town : hs) {
				group_num = getGroup(checked,town);
				checked_town1 = Math.max(group_num,checked_town1);
				town1 = town;
			}
			for(int town : hs) {
				set(checked,town,checked_town1);
			}
			hs = map.get(ms.index-ms.value);
			for(int town : hs) {
				group_num = getGroup(checked,town);
				checked_town2 = Math.max(group_num,checked_town2);
				town2 = town;
			}
			for(int town : hs) {
				set(checked,town,checked_town2);
			}

			if(getGroup(checked,town1)!=getGroup(checked,town2)) {
				result += ms.value;
				//System.out.println(ms.value+","+result);
				hs = map.get(ms.index);
				int new_group = getGroup(checked,town1);
				for(int town : hs) {
					set(checked,town,new_group);
				}
				hs = map.get(ms.index-ms.value);
				for(int town : hs) {
					set(checked,town,new_group);
				}
			}
		}

		System.out.println(result);
	}

	public static int getGroup(int[] _checked, int _index) {
		if(_index == _checked[_index]) {
			return _index;
		}else {
			int result = getGroup(_checked,_checked[_index]);
			_checked[_index] = result;
			return result;
		}
	}

	public static void set(int[] _checked, int _index, int _value) {
		int next = _checked[_index];
		if(next!=_index) set(_checked,next,_value);
		_checked[_index] = _value;
	}
}

class MySet implements Comparable{
	boolean isTate;
	int index;
	int value;

	public MySet(boolean _isTate, int _index, int _value) {
		isTate = _isTate;
		index = _index;
		value = _value;
	}

	@Override
	public int compareTo(Object _o) {
		return this.value - ((MySet)_o).value;
	}
}