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()

def main():

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

    ans = 0

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

        a = AB[i * 2 + 0]

        b = AB[i * 2 + 1]

        ans += (b - a - ans) % b

    print(ans)



if __name__ == "__main__":

    main()

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()

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


if __name__ == "__main__":
    main()

-    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 +    for i in range(N - 1, -1, -1): +        a = AB[i * 2 + 0] +        b = AB[i * 2 + 1] +        ans += (b - a - ans) % b