require 'set'
N, M = gets.chomp.split(' ').map(&:to_i)
xyzes = M.times.map { gets.chomp.split(' ').map(&:to_i) }

groups = []
xyzes.each do |a, b, _|
  pair = Set[a,b]
  f = groups.each_with_index do |group, i|
    diff = (group & pair)
    unless diff.empty?
      groups[i] += pair
      break false
    end
  end
  groups << pair if f
end

puts (N - groups.map(&:size).reduce(:+)) + groups.size