open String
open List



let rec my_split_on_char ?(list=[]) char string =
 try let n = index string char in
  my_split_on_char ~list:((sub string 0 n) :: list) char (sub string (n + 1) ((String.length string) - n - 1))
 with Not_found -> rev (string :: list);;


let read_list () =
 my_split_on_char ' ' (read_line ());;

let read_list_int () =
 map int_of_string (read_list ());;

let rec read_input_list ?(list=[]) t = match t with
   0 -> list
 | _ -> read_input_list ~list:((read_list_int ()) :: list) (t - 1);;



let rec solve input k n = match input with
   [] -> k
 | [p; c] :: input ->
           if (n - k + 1 <= c) then
            solve input (k - p + 1) n
           else if (n - k + 1 <= (c + p - 1)) then
            solve input (k + c) n
           else
            solve input k n
 | x -> exit 0;;

let rec c_solve () =
 let list = read_list_int () in match list with
   [n; r] ->
       if (n == 0) then ()
       else
        let input = read_input_list r in
         (print_int (solve input n n); print_newline (); c_solve ())
 | x -> exit 0;;

c_solve ();;

