import java.util.*
import kotlin.random.Random

fun main() {
    val (n, m) = readInts()

    val graph = Array(n + 1) { Vertex(it) }

    repeat(m) {
        val (a, b) = readInts()

        graph[a].adj.add(graph[b])
        graph[b].adj.add(graph[a])
    }


    println(solve(graph))

    //val maxN = (1e5 + 1).toInt()

/*
    val maxN = (100000).toInt()

    var n: Int = 0
    var m: Int = 0
    var con: MutableList<Pair<Int, Int>> = mutableListOf()

    try {
        repeat(10000) { i ->
            println("$i: ======")

            */
/*n = Random.nextInt(2, maxN)
            m = Random.nextInt(2, maxN)*//*


            n = maxN
            m = maxN - 1

            //println("$n $m")
            val graph = Array(n + 1) { Vertex(it) }

            con = mutableListOf<Pair<Int, Int>>()

            repeat(m) { i ->
                */
/*val a = Random.nextInt(1, n + 1)
                val b = Random.nextInt(1, n + 1)*//*

                val a = i + 1
                val b = i + 2
                con.add(a to b)
                //  println("$a $b")

                graph[a].adj.add(graph[b])
                graph[b].adj.add(graph[a])
            }


            val ans = solve(graph)
            println(ans)

        }
    } catch (e: StackOverflowError) {
        println("overflow")
        println("$n $m")
        println(con)
    }
*/
}

private fun solve(graph: Array<Vertex>): Int {
    var connected = 0
    for (t in graph.drop(1)) {
        if (t.used) continue
        connected++
        t.dfs()
    }
    return connected - 1
}


private data class Vertex(val n: Int) {
    fun dfs() {
        val x = LinkedList<Vertex>()
        x.push(this)

        while(x.size > 0) {
            val u = x.pop()
            u.used = true
            for(v in u.adj) {
                if(!v.used) x.push(v)
            }
        }
    }

    val adj = mutableListOf<Vertex>()
    var used = false
}

private fun readInts() = readLine()!!.split(" ").map { it.toInt() }
private fun readLongs() = readLine()!!.split(" ").map { it.toLong() }