import Control.Monad( replicateM)
main = do
    n_ <- readLn::IO Int
    let
	readPair = do
	    [a,b] <- return . map read . words =<< getLine
	    return (b,a)
    bas_ <- replicateM n_ readPair
    let
	proc::Integer->[(Integer,Integer)]->[Integer]
	proc _ [] = []
	proc add ((b,a):bas) = let d = diff b (a+add)
	    in
		d:(proc (d+add) bas)
	  where
	    diff b a = case a `mod` b of
			0   -> 0
			md  -> b-md
    print . sum . proc  0 $ reverse bas_