import scala.annotation.tailrec

object Main extends App {
  @tailrec
  def func(routes:Array[Array[Int]],bridges:Array[Int],count:Int):Int = {
    if (routes.isEmpty||bridges.isEmpty){
      count
    } else {
      val stat = bridges.map(r=>{
        (r,routes.count(rr=>rr.contains(r)))
      })
      val removeBridge = stat.maxBy(_._2)._1
      val newBridges = stat.filter(_._2>0).map(_._1).filter(_!=removeBridge)
      val newRoutes = routes.filter(!_.contains(removeBridge))
      func(newRoutes,newBridges,count+1)
    }
  }
  val scanner = new java.util.Scanner(System.in)
  val n = scanner.nextInt()
  val m = scanner.nextInt()
  val ab = Array.fill(m,2)(scanner.nextInt())
  val routes = ab.map(abi=>{
    (abi(0) until abi(1)).toArray
  })
  val bridges = routes.flatten.distinct
  println(func(routes,bridges,0))
}