require 'set'
n, m = gets.split.map(&:to_i)
g = {}
m.times do
    i, j = gets.split.map(&:to_i)
    (g[i] ||= Set.new) << j
    (g[j] ||= Set.new)  << i
end

component = {}
c = 0
(1..n).each do |i|
    next if !g[i] || component[i]
    queue = [i]
    component[i] = c
    until queue.empty?
        j = queue.shift
        g[j].each do |k|
            if ! component[k]
                component[k] = c
                queue << k
            end
        end
    end
    c += 1
end

p (c - ((1..n).to_a - component.keys).size).abs

