import Control.Monad (replicateM)
import Data.ByteString.Char8 as C (readInteger, words, getLine)
import Data.Maybe (fromJust)

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 (fst . fromJust . C.readInteger) . C.words) <$> C.getLine) :: IO [[Integer]]
    print $ f ab 0