import Control.Applicative

main = do
  _ <- getLine
  dat <- map (map read . words) . lines <$> getContents :: IO [[Int]]
  print $ solve (reverse dat) 0

  where
    solve [] i = i
    solve ([a,b]:xs) i = solve xs (i + if x == 0 then 0 else b - x)
      where
        x = (a+i) `mod` b
        
