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 = in.take(r).toList.reverse.foldLeft(0)((r, i) =>
            if (i >= r(0) - 1 + r(1)) i
            else if (i >= r(1)) i - r(1)
            else i + r(0) - 1 
        )
        println(n - m)
    }
}