module M = Map.Make (struct type t = int let compare = compare end)

let () =
    let main () =
        let n, m = Scanf.sscanf (read_line ()) "%d %d" (fun n m -> n, m) in
        let arr = Array.init m (fun _ -> Scanf.sscanf (read_line ()) "%d %d %d" (fun x y _ -> x, y)) in
        let map =
            Array.fold_left (fun map (x, y) ->
                let map = M.add x (y :: try M.find x map with _ -> []) map in
                          M.add y (x :: try M.find y map with _ -> []) map) M.empty arr
        in
        let vertex = Array.make n 0 in

        let rec dfs cur group =
            if vertex.(cur - 1) = 0 then (
                vertex.(cur - 1) <- group;
                List.iter (fun x -> dfs x group) (try M.find cur map with _ -> [])
            )
        in

        let rec loop i group =
            if i > n then group - 1 else
                if vertex.(i - 1) > 0 then loop (i + 1) group else
                (dfs i group; loop (i + 1) (group + 1))
        in
        let ans = loop 1 1 in
        Printf.printf "%d\n" ans
    in
    main ()