let (n, m) = Scanf.sscanf (read_line ()) "%d %d" @@ fun n m -> (n, m)
let arr = Array.init m @@ fun _ -> Scanf.sscanf (read_line ()) "%d %d %d" @@ fun x y z -> (x - 1, y - 1, z)

let h = (fun tbl -> Array.iter (fun (x, y, _) -> Hashtbl.add tbl x y; Hashtbl.add tbl y x) arr; tbl) (Hashtbl.create n)

let f arr i g =
  let que = Queue.create () in
  Queue.add i que;
  while not (Queue.is_empty que) do
    let i = Queue.pop que in
    arr.(i) <- g;
    List.iter (fun i -> if arr.(i) = -1 then Queue.add i que) @@ Hashtbl.find_all h i;
  done

let () =
  let arr = Array.make n (-1) in
  let ans = ref 0 in
  for i = 0 to n - 1 do
    if arr.(i) = -1 then begin
      f arr i !ans;
      ans := !ans + 1
    end
  done;
  Printf.printf "%d\n" !ans