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: Accepted)

import java.io.InputStream

val input = FastScanner()

fun main(args: Array<String>) = input.run {
    val n = nextInt()
    val a = Array<Pair<Int, Int>>(n) { nextInt() to nextInt() }

    val xi = a.indices.sortedBy { a[it].first }
    val yi = a.indices.sortedBy { a[it].second }

    val edges = mutableListOf<Pair<Int, Int>>()

    (1 until n).forEach { edges += xi[it - 1] to xi[it] }
    (1 until n).forEach { edges += yi[it - 1] to yi[it] }

    fun Pair<Int, Int>.dist(): Int {
        val (x1, y1) = a[first]
        val (x2, y2) = a[second]
        return Math.min(Math.abs(x1 - x2), Math.abs(y1 - y2))
    }

    edges.sortBy { it.dist() }

    val ds = UnionFind(n)

    var ans = 0L

    edges.forEach {
        val (s, t) = it
        if (ds.find(s) != ds.find(t)) {
            ans += it.dist()
            ds.union(s, t)
        }
    }

    println(ans)
}

class FastScanner(private val input: InputStream = System.`in`) {
    private val sb = StringBuilder()
    private val buffer = ByteArray(4096)
    private var pos = 0
    private var size = 0

    fun nextLong(): Long {
        var c = skipWhitespace()

        val sign = if (c == '-'.toInt()) {
            c = read()
            -1
        } else 1

        var ans = 0L

        while (c > ' '.toInt()) {
            ans = ans * 10 + c - '0'.toInt()
            c = read()
        }

        return sign * ans
    }

    fun nextInt() = nextLong().toInt()

    private fun skipWhitespace(): Int {
        while (true) {
            val c = read()
            if (c > ' '.toInt() || c < 0) return c
        }
    }

    private fun read(): Int {
        while (pos >= size) {
            if (size < 0) return -1
            size = input.read(buffer, 0, buffer.size)
            pos = 0
        }
        return buffer[pos++].toInt()
    }
}

class UnionFind(size: Int) {
    private val parent: IntArray = IntArray(size) { it }
    private val rank = IntArray(size)

    fun find(u: Int): Int {
        if (parent[u] != u) {
            parent[u] = find(parent[u])
        }
        return parent[u]
    }

    fun union(u: Int, v: Int): Int {
        val uRoot = find(u)
        val vRoot = find(v)
        if (uRoot == vRoot) {
            return uRoot
        }
        if (rank[uRoot] == rank[vRoot]) {
            rank[uRoot]++
        }
        return if (rank[uRoot] < rank[vRoot]) {
            parent[uRoot] = vRoot
            vRoot
        } else {
            parent[vRoot] = uRoot
            uRoot
        }
    }
}