import Control.Monad (replicateM)

f :: [[Integer]] -> Integer -> Integer
f [] _ = 0
f ([a, b]:abs) acc =
    c + f abs (acc + c)
  where c = (-a - acc) `mod` b

main = do
    n <- readLn :: IO Int
    ab <- reverse <$> replicateM n ((map read . words) <$> getLine) :: IO [[Integer]]
    print $ f ab 0