N, M = gets.split.map(&:strip).map(&:to_i)
A = Array.new(M,0)
B = Array.new(M,0)

g = {}
gg = 0
d = {}
p = {}
M.times do |i|
  a, b = gets.split.map(&:strip).map(&:to_i)
  A[i], B[i] = a, b
  p[b] = a
  # if g[a]
  #   if g[b]
  #     if g[a] != g[b]
  #       if !d[a*1000000+b]
  #         d[a*1000000+b] = true
  #       end
  #     end
  #   else
  #     g[b] = g[a]
  #   end
  # else
  #   if g[b]
  #     g[a] = g[b]
  #   else
  #     gg += 1
  #     g[a] = g[b] = gg
  #   end
  # end
end

c = 0
N.times do |i|
  if !p[i]
    c += 1
  end
end

# puts (N- g.length) + gg - d.length - 1
puts c - 1
