Scanf.scanf "%d" @@ fun n ->
  let ab = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun x y -> x,y)) in
  let ab = Array.init n (fun i -> ab.(n-1-i)) in
  Array.fold_left (fun (s,d) (x,y) ->
    let p = ((x+d+y-1)/y*y - (x+d)) mod y in (s+p, d+p)) (0,0) ab
  |> fst |> Printf.printf "%d\n"