N,M,*xyzs=$<.read.split.map(&:to_i)

class UnionFind
  attr_reader :parents

  def initialize(n)
    @parents = [*0..n]
  end

  def find(x)
    @parents[x] == x ? x : @parents[x] = find(@parents[x])
  end

  def union(x,y)
    @parents[find(y)] = find(x)
  end
end

t = UnionFind.new(N)

xyzs.each_slice(3).each do |x,y,z|
  t.union(x,y)
end

p t.parents.uniq.count - 1


