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

g = {}
gg = 0
d = {}
M.times do |i|
  a, b = gets.split.map(&:strip).map(&:to_i)
  A[i], B[i] = a, b
  if g[a]
    if g[b]
      if g[a] != g[b]
        if !d[g[a]*1000000+g[b]]
          d[g[a]*1000000+g[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

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