import           Control.Monad
import qualified Data.ByteString.Char8       as BS
import           Data.Maybe
import qualified Data.Vector.Unboxed         as V
import qualified Data.Vector.Unboxed.Mutable as VM

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

main = do
  n <- getInt
  as <- VM.new n
  bs <- VM.new n

  forM_ [1..n] $ \i -> do
    [a, b] <- getIntList
    VM.write as (n-i) a
    VM.write bs (n-i) b

  bs' <- V.freeze bs

  let solve i offset res | i == n    = return res
                         | otherwise = do
                             a <- VM.read as i
                             let b = bs' V.! i
                                 r = (a + offset) `mod` b
                                 r' = if r == 0 then 0 else b - r
                             solve (i+1) (offset+r') (res+r')

  ans <- solve 0 0 0
  print ans