N, M = gets.split.map(&:to_i)
U = Array.new(N + 1){|i| i <= N ? i : i - N }
class << U
  def root(i)
    if self[i] != i
      self[i] = root(self[i])
    else
      i
    end
  end
  def union(i,j)
    self[root(i)] = root(j)
  end
end

F = M.times.map{ gets.split.map(&:to_i) }
F.each do |i,j,z|
  U.union(i, j)
end

puts (1 .. N).group_by{|i| U.root(i) }.size


