N = int(eval(input()))

ABlist = [tuple(map(int, input().split())) for _ in range(N)]

ans = 0

for a, b in ABlist[::-1]:

    a += ans

    if a % b == 0:

        continue

    else:

        ans += (b - a % b)

print(ans)

import sys

input = sys.stdin.readline



N = int(eval(input()))

ABlist = [tuple(map(int, input().split())) for _ in range(N)]

ans = 0

for a, b in reversed(ABlist):

    if (a + ans) % b == 0:

        continue

    else:

        ans += b - (a + ans) % b

print(ans)

N = int(eval(input()))
ABlist = [tuple(map(int, input().split())) for _ in range(N)]
ans = 0
for a, b in ABlist[::-1]:
    a += ans
    if a % b == 0:
        continue
    else:
        ans += b - a % b
print(ans)

import sys

input = sys.stdin.readline
N = int(eval(input()))
ABlist = [tuple(map(int, input().split())) for _ in range(N)]
ans = 0
for a, b in reversed(ABlist):
    if (a + ans) % b == 0:
        continue
    else:
        ans += b - (a + ans) % b
print(ans)

+import sys + +input = sys.stdin.readline -for a, b in ABlist[::-1]: -    a += ans -    if a % b == 0: +for a, b in reversed(ABlist): +    if (a + ans) % b == 0: -        ans += b - a % b +        ans += b - (a + ans) % b