object Main extends App {
    val Array(n, m) = io.StdIn.readLine.split(" ").map(_.toInt)
    val lrs = for (_ <- 1 to m) yield io.StdIn.readLine.split(" ").map(_.toInt)

    // (L_i to R_i)
    val indexedseqs = lrs.map(lr => lr.head to lr.last).map(_.toIndexedSeq)
    // compute intersect
    val numberPassAllGate = indexedseqs.reduce((a,b) => a.intersect(b))

    println(numberPassAllGate.length)
}