n, m = gets.chomp!.split(" ").map!(&:to_i)
xyz = Array.new(m) {|index| Array.new(0, m) }

xyzs = {}
xyz.each.with_index(1) do |xyz, i|
    x_i, y_i, z_i = gets.chomp!.split(" ").map!(&:to_i)
    xyzs[i] = {
        x: x_i,
        y: y_i,
        z: z_i,
    }
end

$dic = {}
require 'pp'
1.upto(n) do |i, _|
    $dic[i] = nil
end


def find_next(i, count = 0)
    if $dic[i].nil?.!
        find_next($dic[i])
    else
        return i, count
    end
end

xyzs.each do |i, xyz2|
    i, _ = find_next(xyz2[:x])
    $dic[i] = xyz2[:y]
end

count = 0

$dic.count do |key, val|
    if val == nil || key == val 
        count+= 1
    end
end

puts count
