let n = Scanf.scanf " %d" (+) 0
let ab_s = 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 if a < b then b - a else (a + b - 1) / b * b - a
let ans = ref 0
let _ = for i = n - 1 downto 0 do ans := !ans + f (fst ab_s.(i) + !ans) (snd ab_s.(i)) done; Printf.printf "%d\n" !ans