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)

fn main() {
    use std::io::Read;
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut iter = buf.split_whitespace();
    
    let n: usize = iter.next().unwrap().parse::<usize>().unwrap();
    let mut cities = vec![];
    for i in 0..n {
        let x: u64 = iter.next().unwrap().parse::<u64>().unwrap();
        let y: u64 = iter.next().unwrap().parse::<u64>().unwrap();
        cities.push((y,x,i));
    }

    let mut edges = vec![];
    // sort by y
    cities.sort_by_key(|c| c.0);
    for i in 0..n-1 {
        let cur = cities[i];
        let next = cities[i+1];
        edges.push((next.0 - cur.0, cur.2, next.2));
    }
    cities.sort_by_key(|c| c.1);
    for i in 0..n-1 {
        let cur = cities[i];
        let next = cities[i+1];
        edges.push((next.1 - cur.1, cur.2, next.2));
    }

    println!("{}", kruskal(&mut edges, n));
}

pub fn kruskal(edges: &mut [(u64, usize, usize)], n: usize) -> u64 {
    edges.sort();
    let mut utree = Utree::new(n);
    let mut ret = 0u64;
    for &(c, p, q) in &*edges {
        if !utree.is_same(p, q) {
            utree.unite(p, q);
            ret += c;
        }
    }
    ret
}

#[allow(dead_code)]
pub struct Utree {
    n: usize,
    par: Vec<usize>,
    rank: Vec<usize>,
    size: Vec<usize>,
}
#[allow(dead_code)]
impl Utree {
    pub fn new(n: usize) -> Utree {
        let par: Vec<usize> = (0..n).collect();
        let rank: Vec<usize> = vec![0; n];
        let size: Vec<usize> = vec![1; n];
        Utree {
            n: n,
            par: par,
            rank: rank,
            size: size,
        }
    }
    pub fn find(&mut self, x: usize) -> usize {
        if self.par[x] == x {
            x
        } else {
            let parent = { self.par[x] };
            let ret = self.find(parent);
            self.par[x] = ret;
            ret
        }
    }
    pub fn unite(&mut self, x: usize, y: usize) {
        let x_root = self.find(x);
        let y_root = self.find(y);
        if x_root == y_root {
            return;
        };
        if self.rank[x_root] < self.rank[y_root] {
            self.par[x_root] = y_root;
            self.size[y_root] += self.size[x_root];
        } else {
            self.par[y_root] = x_root;
            self.size[x_root] += self.size[y_root];
            if self.rank[x_root] == self.rank[y_root] {
                self.rank[x_root] += 1;
            }
        }
    }
    pub fn is_same(&mut self, x: usize, y: usize) -> bool {
        let x_root = self.find(x);
        let y_root = self.find(y);
        x_root == y_root
    }
    pub fn get_size(&mut self, x: usize) -> usize {
        let x_root = self.find(x);
        self.size[x_root]
    }
}