module AtCoder
  class DSU
    def initialize(n = 0)
      @n = n
      @parent_or_size = Array.new(n, -1)
    end

    def merge(a, b)
      x, y = leader(a), leader(b)
      return x if x == y

      x, y = y, x if -@parent_or_size[x] < -@parent_or_size[y]
      @parent_or_size[x] += @parent_or_size[y]
      @parent_or_size[y] = x
    end

    def same(a, b)
      leader(a) == leader(b)
    end

    def leader(a)
      return a if @parent_or_size[a] < 0

      @parent_or_size[a] = leader(@parent_or_size[a])
    end

    def size(a)
      -@parent_or_size[leader(a)]
    end

    def groups
      @n.times.group_by { |i| leader(i) }.values
    end
  end
end

n, m = gets.split.map!(&:to_i)

dsu = AtCoder::DSU.new(n + 1)
m.times do
  a, b = gets.split.map!(&:to_i)
  dsu.merge(a, b)
end

puts dsu.groups.size - 2
