#!/usr/bin/env python3

(n, ), *s = [[*list(map(int, i.split()))] for i in open(0)]

a, b = list(zip(*s))

c = 0

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

    q = (a[i] + c) % b[i]

    c += b[i] - q if q > 0 else 0

print(c)

#!/usr/bin/env python3

(n, ), *s = [[*list(map(int, i.split()))] for i in open(0)]

c = 0

for a, b in reversed(s):

    q = (a + c) % b

    c += b * (q > 0) - q

print(c)

#!/usr/bin/env python3
(n,), *s = [[*list(map(int, i.split()))] for i in open(0)]
a, b = list(zip(*s))
c = 0
for i in reversed(list(range(n))):
    q = (a[i] + c) % b[i]
    c += b[i] - q if q > 0 else 0
print(c)

#!/usr/bin/env python3
(n,), *s = [[*list(map(int, i.split()))] for i in open(0)]
c = 0
for a, b in reversed(s):
    q = (a + c) % b
    c += b * (q > 0) - q
print(c)

-a, b = list(zip(*s)) -for i in reversed(list(range(n))): -    q = (a[i] + c) % b[i] -    c += b[i] - q if q > 0 else 0 +for a, b in reversed(s): +    q = (a + c) % b +    c += b * (q > 0) - q