object Main extends App {
    val in = io.Source.stdin.getLines.map(_.split(" ").map(_.toInt))
    for (Array(n, r) <- in if n > 0 && r > 0) {
        val m = n - in.take(r).foldRight(0){
            case (Array(p, c), i) => if (i >= p - 1 + c) i else if (i >= c) i - c else i + p - 1 
        }
        println(n - m)
    }
}