class UnionFind
  def initialize(n)
    @data = Array.new(n, &:itself)
  end

  def num_roots
    @data.uniq.size
  end

  def root(i)
    _compact!(i, _root(i))
  end

  def same?(i, j)
    root(i) == root(j)
  end

  def unite!(i, j)
    _compact!(i, root(j))
  end

  def _root(i)
    i = @data[i] until i == @data[i]
    i
  end

  def _compact!(i, r)
    until @data[i] == r
      tmp = @data[i]
      @data[i] = r
      i = tmp
    end
    r
  end
end

n, m = gets.chomp.split.map(&:to_i)
uf = UnionFind.new(n)
m.times do
  a, b = gets.chomp.split.map(&:to_i)
  uf.unite!(a-1, b-1)
end
puts uf.num_roots - 1
