class UnionFind
  attr_reader :parent
  attr_reader :rank

  def initialize(size)
    @rank = Array.new(size, 0)
    @parent = Array.new(size, -1)
  end

  def find(x)
    return x if (@parent[x]).negative?

    @parent[x] = find(@parent[x])
    @parent[x]
  end

  def union(x, y)
    px = find(x)
    py = find(y)

    return if px == py

    px, py = py, px if @parent[px] > @parent[py]
    @parent[px] += @parent[py]
    @rank[px] += 1
    @rank[py] = 0
    @parent[py] = px
  end

  def roots
    @parent.each_index.select { |i| @parent[i].negative? }
  end

  def count_groups
    roots.length
  end

  def same_parent?(x, y)
    find(x) == find(y)
  end
end

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

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

puts uf.count_groups - 2
