n, m = gets.chomp.split.map(&:to_i)
graph = Hash.new{ |h,k| h[k] = {} }

m.times do |i|
  x, y, z = gets.chomp.split.map(&:to_i)
  graph[x][y] = z & 1
  graph[y][x] = z & 1
end

count = 0
check = []
(1..n).each do |i|
  next if check[i] != nil
  count += 1
  check[i] = 0
  queue = graph[i].to_a
  while q = queue.shift
    y, z = q
    next if check[y] != nil
    check[y] = z
    graph[y].each do |ny, nz|
      queue << [ny, z ^ nz]
    end
  end
end

puts(count)