import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    n <- getInt
    ab <- replicateM n $ do
        [a,b] <- getIntList
        return (a,b)
    print $ foldr solve 0 ab

solve (a,b) k = k + ((b - ((a+k) `mod` b)) `mod` b)