let get_input n =
	let rec loop n acc =
		if n = 0 then acc
		else 
			read_line ()
			|> Str.split (Str.regexp " ")
			|> (fun [a; b] -> loop (n - 1) ((int_of_string a, int_of_string b) :: acc))
	in	
	loop n []

let solve lst =
	let rec loop lst acc =
		match lst with
		| [] -> acc
		| (a, b) :: xs -> 
			loop xs (acc + (b - (a + acc) mod b) mod b)
	in
	loop lst 0
	
let () =
	let n = read_int () in
	let lst = get_input n in
	solve lst
	|> print_int
	|> print_newline
