n = int(eval(input()))

a = []

b = []

for i in range(n):

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

    a.append(x)

    b.append(y)

a = a[::-1]

b = b[::-1]

cnt = 0

for i in range(n):

    if (a[i]+cnt)%b[i]:

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

print(cnt)
def solve():

    n = int(eval(input()))

    a = []

    b = []

    for _ in range(n):

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

        a.append(x)

        b.append(y)

    ans = 0

    for i in range(n)[::-1]:

        if (a[i]+ans)%b[i]:

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

    print(ans)

if __name__=='__main__':

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

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


if __name__ == "__main__":
    solve()

-n = int(eval(input())) -a = [] -b = [] -for i in range(n): -    x, y = list(map(int, input().split())) -    a.append(x) -    b.append(y) -a = a[::-1] -b = b[::-1] -cnt = 0 -for i in range(n): -    if (a[i] + cnt) % b[i]: -        cnt += b[i] - (a[i] + cnt) % b[i] -print(cnt) +def solve(): +    n = int(eval(input())) +    a = [] +    b = [] +    for _ in range(n): +        x, y = list(map(int, input().split())) +        a.append(x) +        b.append(y) +    ans = 0 +    for i in range(n)[::-1]: +        if (a[i] + ans) % b[i]: +            ans += b[i] - (a[i] + ans) % b[i] +    print(ans) + + +if __name__ == "__main__": +    solve()