let () =
  let n = Scanf.scanf "%d\n" (fun n -> n) in
  let abs = Array.init n (fun _ -> Scanf.scanf "%d %d\n" (fun a b -> a, b)) in
  Array.to_list abs
  |> List.rev
  |> List.fold_left (fun pushed (a, b) -> pushed + if (a + pushed) mod b = 0 then 0 else b - (a + pushed) mod b) 0
  |> Printf.printf "%d\n"
