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)
    }

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

    fun println(line: Long) {
        println(line.toString())
    }

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

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

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

    fun readLong(): Long {
        return readArg().toLong()
    }

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

    fun readLongList(): List<Long> {
        return readArgList().map { it.toLong() }
    }

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

    data class Edge(val pointA: Point, val pointB: Point, val length: Int) {
        fun equals(other: Edge): Boolean {
            return pointA == other.pointA && pointB == other.pointB ||
                    pointA == other.pointB && pointB == other.pointA
        }
    }

    fun solve(): Unit {
        val n = readInt()
        var pointArray = Array<Point>(n, { Point(readIntList(), Tag(it)) })
        
        var edgeArray = arrayOfNulls<Edge>(2 * (n - 1))

        pointArray.sortBy { it!!.x }

        for (i in 0..(n-2)) {
            edgeArray[i] = Edge(
                    pointArray[i], pointArray[i + 1],
                    pointArray[i+1]!!.x - pointArray[i]!!.x)
        }

        pointArray.sortBy { it!!.y }

        for (i in 0..(n-2)) {
            edgeArray[n - 1 + i] = Edge(
                    pointArray[i], pointArray[i + 1],
                    pointArray[i+1]!!.y - pointArray[i]!!.y)
        }

        edgeArray.sortBy { it!!.length }

        var totalCost = 0

        edgeArray.forEach {
            if (it!!.pointA.tag != it.pointB.tag) {
                val tag: Tag
                val replacedTag: Tag
                if (it.pointA.tag.number < it.pointB.tag.number) {
                    tag = it.pointA.tag
                    replacedTag = it.pointB.tag
                } else {
                    tag = it.pointB.tag
                    replacedTag = it.pointA.tag
                }
                pointArray.forEach { sub ->
                    if (sub!!.tag == replacedTag)
                        sub.tag = tag
                }
                totalCost += it.length
            }
        }

        println(totalCost)
    }

    data class Tag(val number: Int)

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