import Control.Monad
import Data.List

main = do
  n <- readLn
  abs <- map (map read . words) <$> replicateM n getLine
  let [as,bs] = transpose abs
  print (marray 0 (reverse as) (reverse bs))

marray acc (a:as) (b:bs) = marray (acc + d) as bs
  where
    d = if r == 0 then 0 else b - r 
    r = mod (a+acc) b
marray acc _ _ = acc