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

class UnionFind
  attr_reader :parents

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

  def find(x)
    if @parents[x] == x
      x
    else
      @parents[x] = find(@parents[x])
    end
  end

  def union(x,y)
    x = find(x)
    y = find(y)
    @parents[y] = x if x != y
  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


