import sys

input = sys.stdin.readline



N = int(eval(input()))

A = [0] * N

B = [0] * N



for i in range(N):

    A[i], B[i] = list(map(int, input().split()))



ans = 0



for i in reversed(list(range(N))):

    if (ans + A[i]) % B[i] != 0:

        ans += ((ans + A[i]) // B[i] + 1) * B[i] - (ans + A[i])



print(ans)
import sys

input = sys.stdin.readline



N = int(eval(input()))

A = [0] * N

B = [0] * N



for i in range(N):

    A[i], B[i] = list(map(int, input().split()))



ans = 0



for i in reversed(list(range(N))):

    t = (ans + A[i]) % B[i]

    if t != 0:

        ans += B[i] - t



print(ans)
import sys

input = sys.stdin.readline
N = int(eval(input()))
A = [0] * N
B = [0] * N
for i in range(N):
    A[i], B[i] = list(map(int, input().split()))
ans = 0
for i in reversed(list(range(N))):
    if (ans + A[i]) % B[i] != 0:
        ans += ((ans + A[i]) // B[i] + 1) * B[i] - (ans + A[i])
print(ans)

import sys

input = sys.stdin.readline
N = int(eval(input()))
A = [0] * N
B = [0] * N
for i in range(N):
    A[i], B[i] = list(map(int, input().split()))
ans = 0
for i in reversed(list(range(N))):
    t = (ans + A[i]) % B[i]
    if t != 0:
        ans += B[i] - t
print(ans)

-    if (ans + A[i]) % B[i] != 0: -        ans += ((ans + A[i]) // B[i] + 1) * B[i] - (ans + A[i]) +    t = (ans + A[i]) % B[i] +    if t != 0: +        ans += B[i] - t