object Main extends App {
  val Array(n, m) = io.StdIn.readLine().split(' ').map(_.toInt)
  val against = (for(_ <- 0 until m toList) yield
    io.StdIn.readLine().split(' ').map(_.toInt) match {
      case Array(a, b) => (a-1, b-1)
    }).sortWith{case (x, y) => (x._2-x._1) < (y._2-y._1)}

  def solver(againstTuples: List[(Int, Int)], broken: List[Int], count: Int): Int = againstTuples match {
    case (a, b) :: tail if !broken.exists(i => a <= i && i < b) =>
      (a until b).map(i => solver(tail, i :: broken, count+1)).min
    case _ :: tail =>
      solver(tail, broken, count)
    case List() =>
      count
  }

  println(solver(against, List.empty[Int], 0))
}