import java.util.Scanner

import scala.collection.mutable
import scala.util.control.Breaks

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()

    val loads = List.fill(m)((sc.nextInt(), sc.nextInt()))

    val store = mutable.ArrayBuffer.fill(n)(false)

    var i = store.indexOf(false)
    var cnt = 0
    while (i != -1) {
      store(i) = true
      loads.foreach { case (a, b) =>
        if (a - 1 == i) {
          store(b - 1) = true
        } else if (b - 1 == i) {
          store(a - 1) = true
        } else {

        }
      }
      cnt = cnt + 1
      i = store.indexOf(false)
    }

    println(cnt - 1)

  }
}
