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 Rust (1.15.1) (judge verdict: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::input;

fn f(n: usize, e: &Vec<Vec<(i64, usize)>>, s: usize) -> i64 {
    let inf = 1_000_000_000_000_000_i64;
    let mut d = vec![inf; n];
    let mut t = vec![false; n];
    let mut pq = std::collections::BinaryHeap::new();
    let mut res = 0;
    d[s] = 0;
    pq.push(std::cmp::Reverse((0, s)));
    while let Some(std::cmp::Reverse((c_u, u))) = pq.pop() {
        if t[u] {
            continue;
        }
        t[u] = true;
        res += c_u;
        for &(c_v, v) in e[u].iter() {
            if t[v] {
                continue;
            }
            pq.push(std::cmp::Reverse((c_v, v)));
        }
    }
    res
}

fn main() {
    input! {
        n: usize,
        xy: [(i64, i64); n],
    };
    let mut e = vec![vec![]; n];
    let mut x = xy
        .iter()
        .enumerate()
        .map(|(i, &(x, _))| (x, i))
        .collect::<Vec<(i64, usize)>>();
    x.sort();
    for w in x.windows(2) {
        match w {
            &[(x_i, i), (x_j, j)] => {
                e[i].push(((x_i - x_j).abs(), j));
                e[j].push(((x_i - x_j).abs(), i));
            }
            _ => unreachable!(),
        }
    }
    let mut y = xy
        .iter()
        .enumerate()
        .map(|(i, &(_, y))| (y, i))
        .collect::<Vec<(i64, usize)>>();
    y.sort();
    for w in y.windows(2) {
        match w {
            &[(y_i, i), (y_j, j)] => {
                e[i].push(((y_i - y_j).abs(), j));
                e[j].push(((y_i - y_j).abs(), i));
            }
            _ => unreachable!(),
        }
    }

    let ans = f(n, &e, 0);
    println!("{}", ans);
}
*/

fn main() {
    let exe = "/tmp/bin953CCE57";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";