require "pp"





class Main
  def run()
    # Read
    n, m = gets.split(" ").map(&:to_i)
    x_y_z_list = []
    m.times do |i|
      x_y_z_list << gets.split(" ").map(&:to_i)
    end

    # Setup
    @union_find = {}
    (1...n).each do |i|
      @union_find[i] = i
    end

    # Build Union Find
    x_y_z_list.each do |x_y_z|
      x, y, z = x_y_z
      @union_find[y] = root(x)
    end
    pp @union_find

    puts @union_find.map{|i, x| i == x}.delete_if{|x| !x}.count
  end

  def root(x)
    return x if x == @union_find[x]
    return @union_find[x] = root(@union_find[x])
  end
end

main = Main.new()
main.run()
