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)

mod reader {
    use std::fmt::Debug;
    use std::str::FromStr;
    use std::collections::VecDeque;
    use std::io::{self, Read};

    fn _read_line() -> String {
        let mut buf = String::new();
        io::stdin().read_to_string(&mut buf);
        buf
    }

    fn _read_vec_deque_string() -> VecDeque<String> {
        _read_line().split_whitespace().map(|s| s.to_string()).collect()
    }

    pub struct Reader {
        buf : VecDeque<String>
    }

    impl Reader {
        pub fn new() -> Reader {
            Reader {buf : VecDeque::new()}
        }

        fn update_buf(&mut self) {
            if self.buf.is_empty() {
                self.buf = _read_vec_deque_string();
            }
        }

        pub fn next<T>(&mut self) -> T where T: FromStr, T::Err: Debug {
            self.update_buf();
            self.buf.pop_front().unwrap().parse::<T>().unwrap()
        }
    }
}

mod data {
    pub struct UnionFind {
        root : Vec<usize>
    }

    impl UnionFind {
        pub fn new(size : usize) -> UnionFind {
            let mut v = Vec::with_capacity(size);
            for i in 0..size {
                v.push(i);
            }
            UnionFind {root : v}
        }

        pub fn find(&mut self, x : usize) -> usize {
            let xroot = self.root[x];
            if x != xroot {
                self.root[x] = self.find(xroot);
            }
            self.root[x]
        }

        pub fn unite(&mut self, x : usize, y : usize) {
            let xroot = self.find(x);
            self.root[xroot] = self.find(y);
        }

        pub fn same(&mut self, x : usize, y : usize) -> bool {
            self.find(x) == self.find(y)
        }
    }
}

fn with_large_stack(func : fn() -> ()) {
    use std::thread::Builder;
    let builder = Builder::new();
    let th = builder.stack_size(10000000);

    th.spawn(move || {
        func();
    }).unwrap().join();
}

fn main() {
    with_large_stack(_main);
}

fn _main() {
    let mut reader = reader::Reader::new();
    
    let n : usize = reader.next();
    type Index = usize;
    let mut vertex : Vec<(i32, i32, Index)> = Vec::with_capacity(n);
    for i in 0..n {
        let x : i32 = reader.next();
        let y : i32 = reader.next();
        vertex.push((x, y, i));
    }

    type Cost = i32;
    let mut edge : Vec<(Index, Index, Cost)> = Vec::with_capacity(n * 2);
    vertex.sort_by(|a, b| a.0.cmp(&b.0));
    for i in 0..n - 1 {
        edge.push((vertex[i].2, vertex[i + 1].2, vertex[i + 1].0 - vertex[i].0));
    }
    vertex.sort_by(|a, b| a.1.cmp(&b.1));
    for i in 0..n - 1 {
        edge.push((vertex[i].2, vertex[i + 1].2, vertex[i + 1].1 - vertex[i].1));
    }

    edge.sort_by(|a, b| a.2.cmp(&b.2));
    let mut uf = data::UnionFind::new(n);
    let mut cost = 0;
    for e in edge {
        if !uf.same(e.0, e.1) {
            uf.unite(e.0, e.1);
            cost += e.2;
        }
    }
    println!("{}", cost);
}