let n = Scanf.scanf " %d" (+) 0
let ab_s = Array.to_list @@ Array.init n @@ fun _ -> Scanf.scanf " %d %d" @@ fun a b -> a, b
let f a b = if a mod b = 0 then 0 else (a + b - 1) / b * b - a
let _ = List.fold_left (fun c (a, b) -> c + f (a + c) b) 0 @@ List.rev ab_s |> Printf.printf "%d\n"