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]
        d[g[a]] ||= {}
        d[g[a]][g[b]] = true
      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

gm = {}
gt = 0
swp = ->(ggg) {
  unless gm[ggg]
    gm[ggg] = gt
    if d[ggg]
      d[ggg].each do |gb, _|
        next if gm[gb]
        swp.(gb)
      end
    end
  end
}

(1..gg).each do |ga|
  gt += 1
  swp.(ga)
end

a = (N- g.length)
c = gt
puts a + c - 1
