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: Accepted)

use std::io::stdin;

struct Dsu{
    par: Vec<usize>,
    rank: Vec<usize>,
}

impl Dsu {
    fn init(n: usize) -> Dsu {
        let mut dsu = Dsu{ 
            par: Vec::with_capacity(n),
            rank: Vec::with_capacity(n),
        };
        for i in 0..n {
            dsu.par.push(i);
            dsu.rank.push(0);
        }
        dsu
    }

    fn root(&mut self, n: usize) -> usize {
        if self.par[n] == n {
            n
        } else {
            let n = n;
            let r = self.root(self.par[n]);
            self.par[n] = r;
            r
        }
    }

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

    fn unite(&mut self, x: usize, y: usize) {
        let x = self.root(x);
        let y = self.root(y);
        if x == y {
            return
        } 

        if self.rank[x] < self.rank[y] {
            self.par[x] = y;
        } else {
            self.par[y] = x;
        }
    }
}


fn main() {
    // 連結なグラフにするための最小の追加辺数を求める
    // -> X個の連結な部分グラフがある場合、X-1個になる

    // 連結成分を扱うのに UNION-FIND という構造が良い
    
    let mut nm = String::new();
    stdin().read_line(&mut nm).unwrap();
    let mut nm = nm.trim().split_whitespace();
    let n = nm.next().unwrap();
    let n: usize = n.parse().unwrap();
    let mut dsu = Dsu::init(n);

    let m = nm.next().unwrap();
    let m: usize = m.parse().unwrap();

    for _ in 0..m {
        let mut ab = String::new();
        stdin().read_line(&mut ab).unwrap();
        let mut ab = ab.split_ascii_whitespace();
        let a = ab.next().unwrap().parse::<usize>().unwrap() - 1;
        let b = ab.next().unwrap().parse::<usize>().unwrap() - 1;
        dsu.unite(a, b);
    }

    let mut cnt = 0;
    // 連結グラフの個数を数える（同一グループの木の数）
    for i in 0..n {
        cnt += if dsu.root(i) == i { 1 } else { 0 }
    }
    
    println!(
        "{}", cnt - 1
    )
}