solve :: Int -> [[Int]] -> Int
solve n [] = n
solve n ([a,b]:xs)
  | f a b /= 0 = solve (n + b - f a b) xs
  | otherwise =	solve n xs
    where f a b = mod (a+n) b

main = getContents >>= print . solve 0 . map (map read . words) . reverse . tail . lines
