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

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
 * Created by dizzyvn on 6/24/17.
 */
public class Main {

    public static void main(String[] args) {

        class ArrayIndexComparator implements Comparator<Integer> {

            private final int[] array;


            public ArrayIndexComparator(int[] array) {
                this.array = array;
            }


            public Integer[] createIndexArray() {
                Integer[] indexes = new Integer[array.length];
                for (int i = 0; i < array.length; i++) {
                    indexes[i] = i; // Autoboxing
                }
                return indexes;
            }


            @Override
            public int compare(Integer idx1, Integer idx2) {
                // Autounbox from Integer to int to use as array indexes
                if (array[idx1] < array[idx2]) return -1;
                if (array[idx1] > array[idx2]) return 1;
                return 0;
            }
        }

        //input
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] X = new int[n];
        int[] Y = new int[n];
        int[] x_cost = new int[n-1];
        int[] y_cost = new int[n-1];

        for (int i = 0; i < n; i++) {
            X[i] = sc.nextInt();
            Y[i] = sc.nextInt();
        }

        //sort
        ArrayIndexComparator xComparator = new ArrayIndexComparator(X);
        ArrayIndexComparator yComparator = new ArrayIndexComparator(Y);
        Integer[] x_index = xComparator.createIndexArray();
        Integer[] y_index = yComparator.createIndexArray();
        Arrays.sort(x_index, xComparator);
        Arrays.sort(y_index, yComparator);


        for (int i = 0; i < n-1; i++) {
            x_cost[i] = X[x_index[i + 1]] - X[x_index[i]];
            y_cost[i] = Y[y_index[i + 1]] - Y[y_index[i]];
        }

        ArrayIndexComparator xCostComparator = new ArrayIndexComparator(x_cost);
        ArrayIndexComparator yCostComparator = new ArrayIndexComparator(y_cost);
        Integer[] xCost_index = xCostComparator.createIndexArray();
        Integer[] yCost_index = yCostComparator.createIndexArray();
        Arrays.sort(xCost_index, xCostComparator);
        Arrays.sort(yCost_index, yCostComparator);

        //process
        int cost = 0;
        boolean[] flag = new boolean[n];
        int x_pos = 0;
        int y_pos = 0;
        while (x_pos < n-1 && y_pos < n-1) {
            int costX = x_cost[xCost_index[x_pos]];
            int costY = y_cost[yCost_index[y_pos]];

            // try to use x
            if (costX < costY) {
                int idx1 = x_index[xCost_index[x_pos]];
                int idx2 = x_index[xCost_index[x_pos]+1];

                if (!flag[idx1] || !flag[idx2]) {
                    cost += costX;
                    flag[idx1] = true;
                    flag[idx2] = true;
                }
                x_pos++;
            }
            // try to use y
            else {
                int idx1 = y_index[yCost_index[y_pos]];
                int idx2 = y_index[yCost_index[y_pos]+1];

                if (!flag[idx1] || !flag[idx2]) {
                    cost += costY;
                    flag[idx1] = true;
                    flag[idx2] = true;
                }
                y_pos++;
            }
        }

        //output
        System.out.println(cost);
    }
}