module Main where

import Control.Applicative
import Control.Monad
import Control.Monad.State
import Data.Int
import qualified Data.ByteString.Lazy.Char8 as L

type Tokenizer = State [L.ByteString]

nextInt :: Tokenizer Int
nextInt = state $ \(s:ss) -> case L.readInt s of
                               Just (x, _) -> (x, ss)
                               Nothing -> error $ "Can't parse Int from: " ++ (L.unpack s)

solve :: [(Int64, Int64)] -> Int64
solve abs = foldr update 0 abs
    where update (a, b) steps | a' == 0 = steps
                              | otherwise = steps + b - a'
              where a' = (a + steps) `mod` b

go :: Tokenizer Int64
go = do
  n <- nextInt
  abs <- replicateM n $ do
           a <- nextInt
           b <- nextInt
           return (fromIntegral a, fromIntegral b)
  return $ solve abs

main :: IO ()
main = do
  contents <- L.words <$> L.getContents
  let result = evalState go contents
  print result
