local mfl, mce = math.floor, math.ceil
local n = io.read("*n")
local a, b = {}, {}
for i = 1, n do
  a[i], b[i] = io.read("*n", "*n")
end
local sum = 0
for i = n, 1, -1 do
  local dst = mce((a[i] + sum) / b[i])
  sum = sum + dst * b[i] - (a[i] + sum)
end
print(sum)
