import Control.Monad( replicateM)
import Data.List( foldl1')
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 d = diff b a
	    in d `seq` d:(proc $ map (fmap (+d)) bas)
	  where
	    diff 1 0 = 1
	    diff 1 a = 0
	    diff b a = let (dv,md) = a `divMod` b
			in if md/=0
			    then b*(dv+1) - a
			    else 0
    print . foldl1' (+) . proc $ reverse bas_