N, M = gets.split.map(&:to_i)
XYZs = readlines.map{|l| l.split.map(&:to_i)}

@p = [*0 .. N]
@r = Array.new(N + 1, 1)

def root(x)
  @p[x] == x ? x : @p[x] = root(@p[x])
end

def unite(x, y)
  x, y = root(x), root(y)
  return if x == y
  if @r[x] < @r[y]
    @p[x] = y
    @r[y] += @r[x]
  else
    @p[y] = x
    @r[x] += @r[y]
  end
end

XYZs.each{|x, y, z| unite(x, y)}
puts @p.drop(1).sort.uniq.size