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 PHP7 (7.0.15) (judge verdict: Wrong Answer)

<?php

ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_WARNING);
ini_set('display_errors', 'Off');

// define('DEBUG', true);
define('DEBUG', false);

fscanf(STDIN, "%d", $N);

$uf = new UnionFind($N);

for ($i = 0; $i < $N; $i++) {
    fscanf(STDIN, "%d %d", $x[$i], $y[$i]);
}

asort($x);
asort($y);

if (DEBUG) {
    var_dump($x);
    var_dump($y);
}
$xk = array_keys($x);
$xv = array_values($x);
$yk = array_keys($y);
$yv = array_values($y);

for ($i = 0; $i < $N - 1; $i++) {
    $p = $xk[$i] . "-" . $xk[$i+1];
    $d = $xv[$i+1] - $xv[$i];
    $xx[$p] = $d;
}
for ($i = 0; $i < $N - 1; $i++) {
    $p = $yk[$i] . "-" . $yk[$i+1];
    $d = $yv[$i+1] - $yv[$i];
    $yy[$p] = $d;
}

asort($xx);
asort($yy);

if (DEBUG) {
    var_dump($xx);
    var_dump($yy);    
}

$rest = $N - 1;
$curX = 0;
$curY = 0;

while($rest > 0) {
    if (!isset($tmpXK)) {
        list($tmpXK, $tmpXV) = each($xx);
    }
    if (!isset($tmpYK)) {
        list($tmpYK, $tmpYV) = each($yy);
    }
    if (DEBUG) echo "XV: " . $tmpXV . " YK:" . $tmpYV . PHP_EOL;
    if ($tmpXV > $tmpYV) {
        list($posY1, $posY2) = explode("-", $tmpYK);
        if (DEBUG) echo "{$posY1}------{$posY2}\n";
        unset($tmpYK);
        if ($uf->Find($posY1) != $uf->Find($posY2)) {
            $uf->Union($posY1, $postY2);            
            $ans += $tmpYV;
            $rest--;
        }
    } else {
        list($posX1, $posX2) = explode("-", $tmpXK);
        if (DEBUG) echo "{$posX1}------{$posX2}\n";
        unset($tmpXK);
        if ($uf->Find($posX1) != $uf->Find($posX2)) {
            $uf->Union($posX1, $postX2);            
            $ans += $tmpXV;
            $rest--;
        }
    }
    // sleep(1);

    if (DEBUG) echo PHP_EOL;
    if (DEBUG) echo "count: " . $tmpXK . "-" . $tmpYK . PHP_EOL;

    if (DEBUG) echo $rest . PHP_EOL;
}

echo $ans . PHP_EOL;

    class UnionFind {

        var $cnt;
        var $data;
        // var $debug = true;
        var $debug = false;

        function __construct($cnt = 10) {
            $this->cnt = $cnt;
            $this->data = array();
            $this->MakeSetAll();
        }

        function MakeSetAll() {
            for ($i = 1; $i <= $this->cnt; $i++) {
                $this->MakeSet($i);
            }
        }

        function DumpAll() {
            if ($this->debug === false) return;
            for ($i = 1; $i <= $this->cnt; $i++) {
                printf("[%d]%d ", $i, $this->Find($i));
            }
            echo "\n";
        }

        function MakeSet($x) {
            if (!isset($this->data[$x])) {
                $this->data[$x] = array('parent' => null, 'rank' => null);
            }
            $this->data[$x]['parent'] = $x;
            $this->data[$x]['rank'] = 0;
        }
 
        function Union($x, $y) {
            $xRoot = $this->Find($x);
            $yRoot = $this->Find($y);
            // echo "xRoot: ".$xRoot. "\n";
            // echo "yRoot: ".$yRoot. "\n";
            // print_r($xRoot);
            // print_r($yRoot);
            if ($xRoot['rank'] > $yRoot['rank']) {
                $this->data[$yRoot]['parent'] = $xRoot;
                if ($this->debug) echo "$yRoot parent changes $xRoot\n";
                // $yRoot['parent'] = $xRoot;
            } elseif ($xRoot['rank'] < $yRoot['rank']) {
                $this->data[$xRoot]['parent'] = $yRoot;
                if ($this->debug) echo "$xRoot parent changes $yRoot\n";
                // $xRoot['parent'] = $yRoot;
            } elseif ($xRoot != $yRoot) {
                $this->data[$yRoot]['parent'] = $xRoot;
                if ($this->debug) echo "$yRoot parent changes $xRoot\n";
                $this->data[$xRoot]['rank']++;
                // $yRoot['parent'] = $xRoot;
                // $xRoot['rank'] = $xRoot['rank'] + 1;
            }
            if ($this->debug) echo "OK\n";
        }

        function Find($x) {
            if ($this->data[$x]['parent'] == $x) {
                return $x;
            } else {
                $this->data[$x]['parent'] = $this->Find($this->data[$x]['parent']);
                return $this->data[$x]['parent'];
            }
        }
    }