class UnionFind
    # ref : https://qiita.com/k_karen/items/5349a25c3eb7b4697f58
    def initialize(size)
        @rank = Array.new(size, 0)
        @parent = Array.new(size, &:itself)
    end
    # attr_accessor :rank, :parent
    def unite(id_x, id_y)
        x_parent = get_parent(id_x)
        y_parent = get_parent(id_y)
        return if x_parent ==y_parent
        if @rank[x_parent] > @rank[y_parent] then
            @parent[y_parent] = x_parent
        else
            @parent[x_parent] = y_parent
            @rank[y_parent] += 1 if @rank[x_parent] == @rank[y_parent]
        end
    end
    def get_parent(id_x)
        @parent[id_x] == id_x ? id_x : (@parent[id_x] = get_parent(@parent[id_x]))
    end
    def same_parent?(id_x, id_y)
        get_parent(id_x) == get_parent(id_y)
    end
    def size
        @parent.map { |id_x| get_parent(id_x) }.uniq.size
    end
end

n, m = gets.chomp.split(' ').map(&:to_i)
roads = Array.new(m){ gets.chomp.split(' ').map(&:to_i) }

cities = UnionFind.new(n + 1)
cities.unite(0, 1)

roads.each do |road|
    a, b = road
    cities.unite(a, b)
end

puts cities.size - 1