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

readInt = fst . fromJust . BS.readInt

readIntList = map readInt . BS.words

getInt = readInt <$> BS.getLine

getIntList = readIntList <$> BS.getLine

getIntNList n = map readIntList <$> replicateM (fromIntegral n) BS.getLine

calc :: [[Int]] -> Int -> Int
calc [] m = 0
calc (xy:xys) m = d + calc xys m'
  where
    x = head xy
    y = last xy
    n = (x + m) `mod` y
    d = if n == 0 then 0 else y - n
    m' = m + d

main = do
  n <- getInt
  xys <- getIntNList n
  let xys' = reverse xys
  print $ calc xys' 0