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 Kotlin (1.0.0) (judge verdict: Time Limit Exceeded)

import java.util.*

open class MainKt {
    companion object Static {
        @JvmStatic fun main(args: Array<String> = arrayOf()) {
            MainKt().solve()
        }
    }

    val scanner = Scanner(System.`in`)

    fun printf(pattern: String, vararg args: Any): Unit {
        println(String.format(pattern, *args))
    }

    open fun println(line: String) {
        kotlin.io.println(line)
    }

    open fun println(line: Int) {
        println(line.toString())
    }

    open fun readLine(): String {
        return scanner.nextLine()
    }

    fun readArg(): String {
        return readLine()
    }

    fun readInt(): Int {
        return readArg().toInt()
    }

    fun readIntList(): List<Int> {
        return readArgList().map { it.toInt() }
    }

    fun readArgList(): List<String> {
        return readLine().split(" ")
    }

    fun solve(): Unit {
        val n = readInt()
        val pointGroupList = mutableListOf<List<Point>>()
        val groupList = mutableListOf<Group>()
        for (i in 1..n) {
            val (x, y) = readIntList()
            groupList.add(Group(mutableListOf(x), mutableListOf(y)))
//            val p = Point(readIntList())
//            pointGroupList.add(listOf(p))
        }

        var totalCost: Int = 0
        while (groupList.count() > 1) {
            for (i in 0..(groupList.count() / 2 - 1)) {
                var cost = -1
                var minIndex = -1
                for (j in 0..(groupList.count() - 1)) {
                    if (i == j) continue
                    val c = d(groupList[i], groupList[j])
                    if (cost == -1) {
                        cost = c
                        minIndex = j
                    } else {
                        if (cost > c) {
                            minIndex = j
                            cost = c
                        }
                    }
                }
                totalCost += cost
                if (minIndex < i) {
                    groupList[minIndex] = Group(
                            groupList[minIndex].xList.union(groupList[i].xList).sorted() as MutableList<Int>,
                            groupList[minIndex].yList.union(groupList[i].yList).sorted() as MutableList<Int>
                    )
                    groupList.removeAt(i)
                } else {
                    groupList[i] = Group(
                            groupList[minIndex].xList.union(groupList[i].xList).sorted() as MutableList<Int>,
                            groupList[minIndex].yList.union(groupList[i].yList).sorted() as MutableList<Int>
                    )
                    groupList.removeAt(minIndex)
                }
            }
        }

        println(totalCost)
    }

    data class Group(
            val xList: MutableList<Int>,
            val yList: MutableList<Int>) {
    }

    data class Point(val x: Int, val y: Int) {
        constructor(list: List<Int>): this(list[0], list[1])
    }

    val pointList = mutableListOf<Point>()

    fun d(a: Point, b: Point): Int {
        return Math.min(Math.abs(a.x - b.x), Math.abs(a.y - b.y))
    }

    fun d(listA: List<Point>, listB: List<Point>): Int {
        var cost: Int = -1
        listA.forEach { a ->
            listB.forEach { b ->
                if (cost == -1) cost = d(a, b)
                else cost = Math.min(cost, d(a, b))
                if (cost == 0) return 0
            }
        }
        return cost
    }

    fun d(groupA: Group, groupB: Group): Int {
        var isFirst = true
        var cost: Int = -1
        groupA.xList.forEach { a ->
            groupB.xList.forEach { b ->
                val c = Math.abs(a - b)
                if (c == 0) return 0
                if (isFirst) {
                    cost = c
                    isFirst = false
                } else cost = Math.min(c, cost)
            }
        }
        groupA.yList.forEach { a ->
            groupB.yList.forEach { b ->
                val c = Math.abs(a - b)
                if (c == 0) return 0
                cost = Math.min(c, cost)
            }
        }
        return cost
    }
}