require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7
class UnionFind
  def initialize(size) @a = Array.new(size, -1) end
  def root(x) @a[x] < 0 ? x : @a[x] = root(@a[x]) end
  def same?(x, y) root(x) == root(y) end
  def size(x) return -@a[root(x)] end

  def unite(x, y)
    x = root(x)
    y = root(y)
    return false if x == y

    x,y = y,x if @a[x] > @a[y]
    @a[x] += @a[y]
    @a[y] = x
    true
  end
end

n,m = gets.chomp.split.map(&:to_i)
uf = UnionFind.new(n+1)

m.times do
  a,b = gets.chomp.split.map(&:to_i)
  uf.unite(a,b)
end

s = Set.new
1.upto(n) do |i|
  s.add uf.root(i)
end

puts s.size - 1
