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

<?php
$n = intval(fgets(STDIN));
for($i = 0; $i < $n; $i++){
    fscanf(STDIN, "%d%d", $x, $y);
    $X[] = $x-1;
    $Y[] = $y-1;
    $orderX[$i] = $i;
    $orderY[$i] = $i;
}
array_multisort($X, $orderX);
array_multisort($Y, $orderY);

$k = new Kruskal($n);
for($i = 0; $i < $n - 1; $i++){
    $k->connect($orderX[$i] + 1, $orderX[$i+1] + 1, abs($X[$i] - $X[$i+1]));
    $k->connect($orderY[$i] + 1, $orderY[$i+1] + 1, abs($Y[$i] - $Y[$i+1]));
}
echo $k->build();

class Kruskal{
    public $I, $J, $L;
    public $uft;
    function __construct($n){
        $this->uft = new UFT($n);
    }
    function connect($i, $j, $l){
        $this->I[] = $i;
        $this->J[] = $j;
        $this->L[] = $l;
    }
    function build(){
        $total = 0;
        array_multisort($this->L, $this->I, $this->J);
        foreach($this->L as $k => $l){
            if($this->uft->getRoot($this->I[$k]) != $this->uft->getRoot($this->J[$k])){
                $this->uft->unite($this->I[$k], $this->J[$k]);
                $total+=$l;
            }
        }
        return $total;
    }
}

class UFT{
    public $parent;
    public $size;
    public $rank;
    public function __construct($size){
        for($i = 1; $i <= $size; $i++){
            $this->size[$i] = 1;
            $this->rank[$i] = 1;
            $this->parent[$i] = 0;
        }
    }
    public function dump(){
        o("--------------");
        o($this->parent);
        o($this->size);
        o($this->rank);
    }
    public function getRoot($i){
        if($this->parent[$i] == 0){
            return $i;
        }else{
            return $this->getRoot($this->parent[$i]);
        }
    }
    public function unite($i, $j){
        $rootI = $this->getRoot($j);
        $rootJ = $this->getRoot($i);
        if($rootJ == $rootI)return false;//元から同じグループ
        if($this->rank[$rootI] > $this->rank[$rootJ])list($rootI, $rootJ) = [$rootJ, $rootI];//Rank(J)>Rank(I)に揃えておく
        $this->parent[$rootI] = $rootJ;
        if($this->rank[$rootI] == $this->rank[$rootJ]){
            $this->rank[$rootJ]++;
        }
        $this->size[$rootJ]+=$this->size[$rootI];
        $this->size[$rootI] = '*';//不要な情報となるので潰す/デバッグ出力向けに*を入れている
        $this->rank[$rootI] = '*';//同上
    }
    public function size($i){return $this->size[$this->getRoot($i)];}
    public function isUnion($i, $j){return $this->getRoot($i) == $this->getRoot($j);}
}