import Control.Applicative

solve :: Integer -> [[Integer]] -> Integer
solve count [] = count
solve count ([a,b]:ablist) = solve (count+c) ablist
  where c = head [b*x-(a+count)|x<-[1..],b*x>=a+count]

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