macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; }
macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { let mut $ var = $ iter . next ( ) . unwrap ( ) . parse ::< usize > ( ) . unwrap ( ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { let $ var = $ iter . next ( ) . unwrap ( ) . parse ::< usize > ( ) . unwrap ( ) ; input_inner ! { $ iter $ ( $ r ) * } } ; }
macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
struct UnionFind {
    parents: Vec<isize>,
}
impl UnionFind {
    fn new(n: usize) -> UnionFind {
        let parents = vec![-1; n];
        UnionFind { parents: parents }
    }
    fn find(&mut self, x: usize) -> usize {
        if self.parents[x] < 0 {
            x
        } else {
            let xx = self.parents[x] as usize;
            let y = self.find(xx);
            self.parents[x] = y as isize;
            y
        }
    }
    fn union(&mut self, x: usize, y: usize) {
        let mut x = self.find(x);
        let mut y = self.find(y);
        if x == y {
            return;
        }
        if self.parents[x] > self.parents[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.parents[x] += self.parents[y];
        self.parents[y] = x as isize;
    }
    fn same(&mut self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }
}
fn dist(x: usize, y: usize) -> isize {
    (x as isize - y as isize).abs()
}
use std::collections::BinaryHeap;
fn main() {
    input! { n, xy: [(usize, usize); n] };
    let mut xi = (0..n).into_iter().collect::<Vec<_>>();
    xi.sort_by_key(|&i| xy[i].0);
    let mut yi = (0..n).into_iter().collect::<Vec<_>>();
    yi.sort_by_key(|&i| xy[i].1);
    let mut heap = BinaryHeap::new();
    for i in 0..(n - 1) {
        let x1 = xi[i];
        let x2 = xi[i + 1];
        heap.push((-dist(xy[x1].0, xy[x2].0), x1, x2));
        let y1 = yi[i];
        let y2 = yi[i + 1];
        heap.push((-dist(xy[y1].1, xy[y2].1), y1, y2));
    }
    let mut ans = 0;
    let mut uf = UnionFind::new(n);
    while let Some((c, i, j)) = heap.pop() {
        if !uf.same(i, j) {
            ans -= c;
            uf.union(i, j);
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 19284