Scanf.scanf "%d %d" (fun n m ->
    let state = Array.make n 10 in
    let sc = Array.init m (fun _ -> Scanf.scanf " %d %d" (fun s c -> s, c)) in
    let rec loop i =
        if i < 0 then (
            match n with
            | 1 -> if state.(0) = 10 then 0 else
                   if state.(0) = -1 then -1 else state.(0)
            | 2 -> if state.(0) = -1 || state.(1) = -1 || state.(0) = 0 then (-1) else (
                if state.(0) = 10 then state.(0) <- 1;
                if state.(1) = 10 then state.(1) <- 0;
                state.(0) * 10 + state.(1)
            )
            | 3 -> if state.(0) = -1 || state.(1) = -1 || state.(2) = -1 || state.(0) = 0 then (-1) else (
                if state.(0) = 10 then state.(0) <- 1;
                if state.(1) = 10 then state.(1) <- 0;
                if state.(2) = 10 then state.(2) <- 0;
                state.(0) * 100 + state.(1) * 10 + state.(2)
            )
            | _ -> failwith "??"
        ) else (
            let s, c = sc.(i) in
            let s = s - 1 in
            let c = if state.(s) = 10 || state.(s) = c then c else (-1) in
            let () = state.(s) <- c in
            loop (i - 1)
        )
    in
    loop (m - 1) |> Printf.printf "%d\n"
)