val p = IntArray(100001 ) { 0 }

fun main(args: Array<String>) {
    val (n, m) = getInts()
    repeat(m) {
        val (x , y) = getInts()
        val px = parent(x)
        val py = parent(y)
        if (px != py) p[py] = px
    }
    println(p.drop(1).take(n).count { it == p[it] } - 1)
}

fun parent(k: Int): Int {
    if (p[k] == 0) return k
    return parent(p[k]).also {
        p[k] = it
    }
}

fun getInt() = readLine()!!.toInt()
fun getInts() = readLine()!!.split(" ").filter { it.isNotBlank() }.map { it.toInt() }