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 OCaml (4.10.0) (judge verdict: Runtime Error)

open Batteries
class union_find n = object (self)
  val mutable par = ( [| |] : int array)
  method init = 
    par <- Array.init n (fun a -> a)
  method get_par = par

  method root s = 
    let rec loop x =
      if par.(x) = x then x else (par.(x) <- loop par.(x); par.(x))
    in loop s

  method rank s =
    let rec loop x n =
      if par.(x) = x then n else loop par.(x) (n+1)
    in loop s 1

  method unite x y =
    let xrank = self#rank x in
    let yrank = self#rank x in
    let rx = self#root x in
    let ry = self#root y in
    if rx != ry then (if xrank < yrank then par.(rx) <- ry else par.(ry) <- rx)

  method same x y = self#root x = self#root y

  method members x = (* 要素xが属する木の全ての要素を返す *) 
    let r = self#root x in Array.fold_left (fun a y -> if self#root y = r then y :: a else a) [] par

  method roots = (* 全ての根 *) Array.fold_left (fun x y -> if List.mem y x then x else y :: x) [] par

  method tree_count = (* 木の数 *) List.length @@ self#roots
end


let n, m = Scanf.sscanf (read_line()) "%d %d" (fun n m -> n,m)
let tree = new union_find n
let _ = tree#init
let rec loop i =
  if i >= m then () else Scanf.sscanf (read_line()) "%d %d" (fun a b -> tree#unite a b; loop (i+1))

let () = loop 0
let rec get_next result =
  if result >= n then -1 else
  if tree#same 0 result then get_next (result+1) else result
let rec tree_loop result =
  let a, b = (0, get_next 0) in
    if b = -1 then result else
    (tree#unite a b; tree_loop (result+1))
let () = Printf.printf "%d\n" @@ tree_loop 0