N,M,*xyzs=$<.read.split.map(&:to_i)
arr = Array.new(N+1) { Array.new }
xyzs.each_slice(3).each do |x,y,z|
  arr[x] << y
end

 = 1
ret = 0
while i <= N
  if arr[i]
    ret += 1

    q = arr[i]
    while !q.empty?
      t = q.pop
      q += arr[t] if arr[t]
      arr[t] = nil
    end
  end
  i += 1
end

p ret


