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)

#![allow(unused_imports, dead_code, unused_variables, unused_mut)]
use num::abs;
use num::integer::gcd;
use permutohedron::LexicalPermutation;
use proconio::{fastout, input, marker::*};
use std::cmp;
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
use std::mem::swap;
use text_io::{read, scan};
const INF: i32 = std::i32::MAX;
const MOD: i32 = 1e9 as i32 + 7;
const Yes: &str = "Yes";
const No: &str = "No";
static dx: [i32; 4] = [1, 0, -1, 0];
static dy: [i32; 4] = [0, -1, 0, 1];

#[derive(Debug)]
struct UnionFind {
    data: Vec<usize>,
    rank: Vec<usize>,
}

// https://pyteyon.hatenablog.com/entry/2019/03/11/200000
impl UnionFind {
    /// return UnionFind  
    /// data was inited by i  
    /// rank was inited by 1
    fn new(n: usize) -> UnionFind {
        UnionFind {
            data: (0..n).map(|i| i).collect::<Vec<usize>>(),
            rank: (0..n).map(|_| 1).collect::<Vec<usize>>(),
        }
    }

    /// return root of x
    fn root(&mut self, x: usize) -> usize {
        if self.data[x] == x {
            return x;
        }
        self.data[x] = self.root(self.data[x]);
        self.data[x]
    }

    /// unite x and y
    fn unite(&mut self, x: usize, y: usize) {
        let mut root_x = self.root(x);
        let mut root_y = self.root(y);

        if root_x == root_y {
            return;
        }

        if self.rank[root_x] < self.rank[root_y] {
            swap(&mut root_x, &mut root_y);
        }
        self.rank[root_x] += self.rank[root_y];
        self.data[root_y] = root_x;
    }

    /// check if x and y belong to same root
    fn same(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }

    /// check size of tree x belongs to
    fn rank(&mut self, x: usize) -> usize {
        let root_x = self.root(x);
        self.rank[root_x]
    }
}

#[fastout]
fn main() {
    input! {
        n: usize,
        m: usize,
        ab: [(i32, i32); m],
    }

    let mut uf = UnionFind::new(n);
    for elem in &ab {
        uf.unite(elem.0 as usize - 1, elem.1 as usize - 1);
    }

    let mut ans = 0;
    let mut min = INF as usize;
    for i in 0..n {
        min = cmp::min(min, uf.root(i));
    }

    for i in 0..n {
        if uf.root(i) == min {
            continue;
        }
        if uf.root(i) == i {
            ans += 1;
        }
    }

    println!("{}", ans);
}