fun main(){
    val (n, m) = readLine()!!.split(" ").map { it.toInt() }
    val tosi = MutableList(n){ MutableList(0){ 0 } }
    for (i in 0 until m){
        val ab = readLine()!!.split(" ").map { it.toInt() }
        tosi[ab[0] - 1].add(ab[1])
        tosi[ab[1] - 1].add(ab[0])
    }
    val flagList = MutableList(n){ 0 }
    val checkList = MutableList(0){ 0 }
    var flag = -1
    tailrec fun check(d: Int){
        for (i in 0 until tosi[d-1].size){
            if (flagList[tosi[d-1][i] - 1] == 0) {
                checkList.add(tosi[d-1][i])
                flagList[tosi[d-1][i] - 1] = 1
            }

        }
        checkList.removeAt(0)
        if (checkList.size > 0) check(checkList[0])
    }
    for (i in 0 until n){
        if (flagList[i] == 0){
            flag++
            flagList[i] = 1
            for (j in 0 until tosi[i].size){
                checkList.add(tosi[i][j])
                flagList[tosi[i][j] - 1] = 1
            }
        }
        if (checkList.size > 0) check(checkList[0])

    }
    println(flag)
}