n, m = gets.chomp.split(" ").map(&:to_i)
xyz = []
m.times do
  x, y, z = gets.chomp.split(" ").map(&:to_i)
  xyz << [x, y, (z % 2 == 0 ? 2 : 1)]
end

require "set"

def find_set(all_sets, x, y)
  all_sets.find { |s| s.include?(x) || s.include?(y) }
end

def compact(all_sets)
  return all_sets if all_sets.size <= 1
  cnt = 0
  new_all_sets = all_sets.map { |s| s.dup }
  0.upto(new_all_sets.size - 2) do |i|
    (i + 1).upto(new_all_sets.size - 1) do |j|
      if (new_all_sets[i] & new_all_sets[j]).size > 0
        new_all_sets[i] = new_all_sets[i] + new_all_sets[j]
        new_all_sets[j].clear
        cnt += 1
      end
    end
  end
  compact(new_all_sets) if cnt > 0
  new_all_sets
end

all_sets = []
found_set = Set.new
m.times do |i|
  x, y, z = xyz[i]
  found_set << x
  found_set << y
  unless find_set(all_sets, x, y)
    s = Set.new
    s << x
    s << y
    all_sets << s
  end
end

all_sets = compact(all_sets)
whole_set = (1..n).to_set
puts (whole_set - found_set).size + all_sets.size
