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
