import sys

import numpy as np

input = sys.stdin.readline



N = int(eval(input()))

A = []

B = []

for _ in range(N):

    a,b = list(map(int,input().split()))

    A.append(a)

    B.append(b)

cnt = 0

for i in range(N-1,-1,-1):

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

        continue

    else:

        cnt += B[i] - (A[i]+cnt)%B[i]

print(cnt)

import sys

input = sys.stdin.readline

N = int(eval(input()))

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

cnt = 0

for i in range(N-1,-1,-1):

    if (AB[i][0]+cnt)%AB[i][1] == 0:

        continue

    else:

        cnt += AB[i][1] - (AB[i][0]+cnt)%AB[i][1]

print(cnt)

import sys
import numpy as np

input = sys.stdin.readline
N = int(eval(input()))
A = []
B = []
for _ in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)
cnt = 0
for i in range(N - 1, -1, -1):
    if (A[i] + cnt) % B[i] == 0:
        continue
    else:
        cnt += B[i] - (A[i] + cnt) % B[i]
print(cnt)

import sys

input = sys.stdin.readline
N = int(eval(input()))
AB = [list(map(int, input().split())) for _ in range(N)]
cnt = 0
for i in range(N - 1, -1, -1):
    if (AB[i][0] + cnt) % AB[i][1] == 0:
        continue
    else:
        cnt += AB[i][1] - (AB[i][0] + cnt) % AB[i][1]
print(cnt)

-import numpy as np -A = [] -B = [] -for _ in range(N): -    a, b = list(map(int, input().split())) -    A.append(a) -    B.append(b) +AB = [list(map(int, input().split())) for _ in range(N)] -    if (A[i] + cnt) % B[i] == 0: +    if (AB[i][0] + cnt) % AB[i][1] == 0: -        cnt += B[i] - (A[i] + cnt) % B[i] +        cnt += AB[i][1] - (AB[i][0] + cnt) % AB[i][1]