object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val N, M = sc.nextInt()
  val (set, groups) = LazyList
    .iterate(List.empty[Int] -> List.empty[Vector[Int]]) {
      case (set, groups) =>
        val a, b = sc.nextInt()
        val (l, r) = groups.partition(g => g.contains(a) || g.contains(b))
        (a :: b :: set) -> ((l.foldLeft(Vector.empty[Int])(_ ++ _) ++ Vector(a, b)).distinct :: r)
    }(M)
  println(N - set.distinct.size + groups.length - 1)
}
