class UFTree
  def initialize(n)
    @p = Array.new(n + 1, -1)
  end
  def find(x)
    @p[x] < 0 ? x : @p[x] = find(@p[x])
  end
  def unite(a, b)
    x = find(a)
    y = find(b)
    if x != y
      x, y = y, x if @p[y] < @p[x]
      @p[y] = x
    end
  end
  def parents
    @p
  end
end

N, M = gets.split.map(&:to_i)

tree = UFTree.new(N)
M.times do
  a, b = gets.split.map(&:to_i)
  tree.unite(a, b)
end
puts tree.parents.count{|x| x < 0} - 2
