n,m = gets.split.map(&:to_i)
ans = 0
check = Array.new(n,-1)
card_s = Array.new(n).map{Array.new()}
card_d = Array.new(n).map{Array.new()}
for i in 0..m-1
  inst = gets.split.map(&:to_i)
  if inst[2] % 2 == 0
    card_s[inst[0]-1].push(inst[1]-1)
    card_s[inst[1]-1].push(inst[0]-1)
  else
    card_d[inst[0]-1].push(inst[1]-1)
    card_d[inst[1]-1].push(inst[0]-1)
  end
end

for i in 0..n-1
  if check[i] == -1
    ans += 1
    check[i] = 0
    if card_s[i].length != 0
      for j in 0..card_s[i].length-1
        check[card_s[i][j]] = 0
      end
    end
    if card_d[i].length != 0
      for j in 0..card_d[i].length-1
        check[card_d[i][j]] = 1
      end
    end
  else
    if card_s[i].length != 0
      for j in 0..card_s[i].length-1
        check[card_s[i][j]] = check[i]
      end
    end
    if card_d[i].length != 0
      for j in 0..card_d[i].length-1
        check[card_d[i][j]] = (check[i]-1).abs
      end
    end
  end
end
puts ans
