N, *AB = list(map(int, open(0).read().split()))

ans = 0

for i in range(N):

    index = N - 1 - i

    a = AB[index * 2 + 0] + ans

    b = AB[index * 2 + 1]

    remainder = a % b

    ans += (b - remainder) % b

print(ans)

def main():

    N, *AB = list(map(int, open(0).read().split()))

    ans = 0

    for i in range(N):

        index = N - 1 - i

        a = AB[index * 2 + 0] + ans

        b = AB[index * 2 + 1]

        remainder = a % b

        ans += (b - remainder) % b

    print(ans)



if __name__ == "__main__":

    main()

N, *AB = list(map(int, open(0).read().split()))
ans = 0
for i in range(N):
    index = N - 1 - i
    a = AB[index * 2 + 0] + ans
    b = AB[index * 2 + 1]
    remainder = a % b
    ans += (b - remainder) % b
print(ans)

def main():
    N, *AB = list(map(int, open(0).read().split()))
    ans = 0
    for i in range(N):
        index = N - 1 - i
        a = AB[index * 2 + 0] + ans
        b = AB[index * 2 + 1]
        remainder = a % b
        ans += (b - remainder) % b
    print(ans)


if __name__ == "__main__":
    main()

-N, *AB = list(map(int, open(0).read().split())) -ans = 0 -for i in range(N): -    index = N - 1 - i -    a = AB[index * 2 + 0] + ans -    b = AB[index * 2 + 1] -    remainder = a % b -    ans += (b - remainder) % b -print(ans) +def main(): +    N, *AB = list(map(int, open(0).read().split())) +    ans = 0 +    for i in range(N): +        index = N - 1 - i +        a = AB[index * 2 + 0] + ans +        b = AB[index * 2 + 1] +        remainder = a % b +        ans += (b - remainder) % b +    print(ans) + + +if __name__ == "__main__": +    main()