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]
	proc [] = []
	proc ((b,a):bas) = let diff = if b>=a then b-a else f b a
	    in diff:(proc $ map (fmap (+diff)) bas)
	  where
	    f b a = let candidate = map (b*) [2..]
			newB = head $ filter (a<=) candidate
		in newB-a
    print . sum . proc $ reverse bas_