object Main extends App {

  val sc = new java.util.Scanner(System.in) 
  val n, m = sc.nextInt()
  val g = Array.fill(m)((sc.nextInt() to sc.nextInt()).toSet)
  var a = (1 to n).toSet
  g.foreach(x => a = a & x)
  println(a.size)
}