n = int(eval(input()))

a = []

b = [] 

for i in range(n):

    x,y = list(map(int,input().split()))

    a.append(x)

    b.append(y)



res = 0



for i in range(n):

    a[n-1-i] += res

    c = a[n-1-i]%b[n-1-i]

    if c != 0:

        res += b[n-1-i]-c



print(res)
import sys

input = sys.stdin.readline





n = int(eval(input()))



a = []

b = []



for i in range(n):

    aa,bb = list(map(int,input().split()))

    a.append(aa)

    b.append(bb)



s = 0

res = 0

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

    a[i] += res



    if a[i]%b[i] == 0:

        continue

    

    res += b[i]-(a[i]%b[i])

print(res)
n = int(eval(input()))
a = []
b = []
for i in range(n):
    x, y = list(map(int, input().split()))
    a.append(x)
    b.append(y)
res = 0
for i in range(n):
    a[n - 1 - i] += res
    c = a[n - 1 - i] % b[n - 1 - i]
    if c != 0:
        res += b[n - 1 - i] - c
print(res)

import sys

input = sys.stdin.readline
n = int(eval(input()))
a = []
b = []
for i in range(n):
    aa, bb = list(map(int, input().split()))
    a.append(aa)
    b.append(bb)
s = 0
res = 0
for i in range(n - 1, -1, -1):
    a[i] += res
    if a[i] % b[i] == 0:
        continue
    res += b[i] - (a[i] % b[i])
print(res)

+import sys + +input = sys.stdin.readline -    x, y = list(map(int, input().split())) -    a.append(x) -    b.append(y) +    aa, bb = list(map(int, input().split())) +    a.append(aa) +    b.append(bb) +s = 0 -for i in range(n): -    a[n - 1 - i] += res -    c = a[n - 1 - i] % b[n - 1 - i] -    if c != 0: -        res += b[n - 1 - i] - c +for i in range(n - 1, -1, -1): +    a[i] += res +    if a[i] % b[i] == 0: +        continue +    res += b[i] - (a[i] % b[i])