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