Problem:
Score : 300 points

Problem Statement

There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M.
Road i connects City A_i and City B_i.

Snuke can perform the following operation zero or more times:

Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities.

After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times).

What is the minimum number of roads he must build to achieve the goal?

Constraints

2 \leq N \leq 100,000

1 \leq M \leq 100,000

1 \leq A_i < B_i \leq N

No two roads connect the same pair of cities.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

Print the answer.

Sample Input 1

3 1
1 2

Sample Output 1

1

Initially, there are three cities, and there is a road between City 1 and City 2.

Snuke can achieve the goal by building one new road, for example, between City 1 and City 3.
After that,

We can travel between 1 and 2 directly.

We can travel between 1 and 3 directly.

We can travel between 2 and 3 by following both roads (2 - 1 - 3).

Solution in Rust (1.42.0) (judge verdict: Time Limit Exceeded)

fn read_multi<T: std::str::FromStr>() -> Vec<T> {
    let mut s = std::string::String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}

fn read_2d<T: std::str::FromStr>(n: i32) -> Vec<Vec<T>> {
    let mut v2 = Vec::new();
    for _ in 0..n {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).ok();
        let v = s.trim().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect();
        v2.push(v);
    }
    v2
}


fn main() {
    //println!("Hello, world!");

    let vn: Vec<i32> = read_multi(); //i32
    //println!("{:?}", vn);

    let vvn: Vec<Vec<i32>> = read_2d(vn[1]); //i32
    //println!("vvn = {:?}", vvn);

    let mut vcity: Vec<i32> = Vec::new();
    vcity.resize(vn[0] as usize, 1); //new_lenの要素数にベクトルを変換。要素が増える場合Tが必要なだけ追加される
    //println!("vcity = {:?}", vcity);

    //let mut ncity = vn[0];
    let mut vcitycluster: Vec<Vec<i32>> = Vec::new();


    for i in 1..vn[0] + 1 { //街ごとに
        let mut cluster: Vec<i32> = [i].to_vec();
        for j in 0..vn[1] { //つながっている道を調査
            if vvn[j as usize].contains(&i) {
                cluster.push(vvn[j as usize][0]);
                cluster.push(vvn[j as usize][1]);
            } //街ごとに道の両端の街リストを作成したら
        }
        cluster.sort();
        cluster.dedup();
        //println!("cluster = {:?}", cluster);
        vcitycluster.push(cluster);
    }
    //vcitycluster.sort();
    //println!("vcitycluster = {:?}", vcitycluster);

    //test
    //println!("grouping = {:?}", grouping(vcitycluster, vn[0]));
    //println!("grouping = {:?}", grouping(vcitycluster, vn[0]).len() - 1);
    println!("{}", grouping(vcitycluster, vn[0]).len() - 1);


}


fn grouping(mut vvn: Vec<Vec<i32>>, num: i32) -> Vec<Vec<i32>> {

    loop {
      
        //println!("vvn = {:?}", vvn);
        let mut vn: Vec<i32> = Vec::new();
        for i in 0..vvn[0].len() {
            vn.push(vvn[0][i]);
        }
        //println!("vn  = {:?}", vn);
        vvn.remove(0);
        //println!("vvn  = {:?}", vvn);
    
        //let mut n: i32 = 0;
        let mut vvnstate: Vec<i32> = Vec::new();
        for _a in 0..vvn.len() {
            vvnstate.push(0);
            //println!("vvnstate75= {:?}", vvnstate);
        }
        let mut vntemp: Vec<i32> = Vec::new();
    
        for b in 0..vn.len() {
            let n: i32 = vn[b as usize];
            //println!("b, vn[b] = {}, {:?}", b, n);
            for j in 0..vvn.len() {
                if vvn[j].contains(&n) {
                    vvnstate[j] = 1;
                    for k in 0..vvn[j].len() {
                        vntemp.push(vvn[j][k]);
                    }
                }
            }
        } 
     
        //println!("vvnstate  = {:?}", vvnstate);
        //println!("vntemp    = {:?}", vntemp);
    
        for l in 0..vntemp.len() {
            vn.push(vntemp[l]);
        }
        //println!("vn  = {:?}", vn);
        vn.sort();
        vn.dedup();
        //println!("vn  = {:?}", vn);
    
        let mut m: usize = vvnstate.len();
        while m != 0 {
            if vvnstate[m - 1] != 0 {
                vvn.remove(m - 1);
            }
            m -= 1
        }

        vvn.push(vn);
    


        
        if num == vvn.concat().len() as i32 { break; }
        //if num == 8 { break; }
    }

    
    vvn
    
}