n, m = gets.split.map(&:to_i)
l = []
m.times do
  x, y, _ = gets.split.map(&:to_i)
  l_idx = nil
  l.each_with_index do |s, idx|
    break l_idx = idx if s.include?(x) || s.include?(y)
  end
  if l_idx
    l[l_idx] = (l[l_idx] + [x, y]).uniq
  else
    l << [x, y]
  end
end
puts n - l.flatten.size + l.size