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)

use io::*;
use std::collections::HashMap;
use std::cmp;

fn main() {
    let input = read_string();
    let mut sc = Scanner::new(&input);
    let n: usize = sc.next();
    let mut xy: Vec<Vec<usize>> = sc.next_mat(2, n);
    for i in 0..n {
        xy[i].insert(0,i);
    }
    let mut es: Vec<(usize,usize,usize)> = Vec::new();
    xy.sort_by_key(|x| x[1]);
    for e in xy.windows(2) {
        es.push((e[1][1] - e[0][1], e[0][0], e[1][0]));
    }
    xy.sort_by_key(|x| x[2]);
    for e in xy.windows(2) {
        es.push((e[1][2] - e[0][2], e[0][0], e[1][0]));
    }
    es.sort_by_key(|x| x.0);
    let mut ans = 0;
    let mut uf: UnionFind<usize> = UnionFind::new();
    for (d,u,v) in es {
        if uf.is_same(u, v) {
            continue;
        }
        ans += d;
        uf.union(u ,v);
        if uf.size() == n && uf.n_unions() == 1 {
            break;
        }
    }
    println!("{}",ans);
}

struct UFNode {
    parent: usize,
    rank: usize
}

struct UnionFind<T> where T: cmp::Eq + std::hash::Hash {
    map: HashMap<T,usize>,
    nodes: Vec<UFNode>,
    n_unions: usize
}

impl<T: cmp::Eq + std::hash::Hash> UnionFind<T> {
    fn new() -> UnionFind<T> {
        UnionFind{map: HashMap::new(), nodes: Vec::new(), n_unions: 0}
    }

    fn new_points(v: Vec<T>) -> UnionFind<T> {
        let mut uf = UnionFind::new();
        for x in v {
            if uf.map.contains_key(&x) {
                continue;
            }
            uf.add_point(x);
        }
        uf
    }

    fn add_point(&mut self, x: T) -> usize {
        let id = self.nodes.len();
        self.map.insert(x,id);
        self.nodes.push(UFNode{parent: id, rank: 0});
        self.n_unions += 1;
        id
    }

    fn get_id(&mut self, x: T) -> usize {
        self.map.get(&x).map(|x| *x).unwrap_or_else(|| self.add_point(x))
    }

    fn find(&mut self, id: usize) -> usize {
        let p = self.nodes[id].parent;
        if p == id {
            return id;
        }
        let pr = self.find(p);
        self.nodes[id].parent = pr;
        pr
    }

    fn union(&mut self, x: T, y: T) {
        let idx = self.get_id(x);
        let idy = self.get_id(y);
        let xp = self.find(idx);
        let yp = self.find(idy);
        if xp == yp {
            return;
        }
        let rx = self.nodes[xp].rank;
        let ry = self.nodes[yp].rank;
        if rx < ry {
            self.nodes[xp].parent = yp;
        } else if rx > ry {
            self.nodes[yp].parent = xp;
        } else {
            self.nodes[yp].parent = xp;
            self.nodes[xp].rank += 1;
        }
        self.n_unions -= 1;
    }

    fn is_same(&mut self, x: T, y: T) -> bool {
        let idx = self.get_id(x);
        let idy = self.get_id(y);
        self.find(idx) == self.find(idy)
    }

    fn size(&self) -> usize {
        self.nodes.len()
    }

    fn n_unions(&self) -> usize {
        self.n_unions
    }
}

pub mod io {
    use std;
    use std::str::FromStr;

    pub struct Scanner<'a> {
        iter: std::str::SplitWhitespace<'a>,
    }

    impl<'a> Scanner<'a> {
        pub fn new(s: &'a str) -> Scanner<'a> {
            Scanner {
                iter: s.split_whitespace(),
            }
        }

        pub fn next<T: FromStr>(&mut self) -> T {
            let s = self.iter.next().unwrap();
            if let Ok(v) = s.parse::<T>() {
                v
            } else {
                panic!("Parse error")
            }
        }

        pub fn next_vec_len<T: FromStr>(&mut self) -> Vec<T> {
            let n: usize = self.next();
            self.next_vec(n)
        }

        pub fn next_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.next()).collect()
        }

        pub fn next_mat<T: FromStr>(&mut self, w: usize, h: usize) -> Vec<Vec<T>> {
            (0..h).map(|_| self.next_vec(w)).collect()
        }

        pub fn next_vec_char(&mut self) -> Vec<char> {
            self.next::<String>().chars().collect()
        }

        pub fn next_mat_char(&mut self, n: usize) -> Vec<Vec<char>> {
            (0..n).map(|_| self.next_vec_char()).collect()
        }
    }

    pub fn read_string() -> String {
        use std::io::Read;

        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }

    pub fn read_line() -> String {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        s.trim_right().to_owned()
    }
}