fun main(args: Array<String>) {
    val scanner = System.`in`.bufferedReader()
    val input = scanner.readLine().split(" ").map(Integer::parseInt)
    val n = input[0]
    val m = input[1]
    val gates = Array(m) {
        scanner.readLine().split(" ").map(Integer::parseInt)
    }
    val min = gates.map { it[0] }
        .max() ?: 0
    val max = gates.map { it[1] }
        .min() ?: 0
    val ans = (1..n).filter { card ->
        card in min..max
    }.count()
    println(ans)
}