import sys





def main() -> None:

    n = int(sys.stdin.readline())

    a = [0] * n

    b = [0] * n

    ans = 0



    for i in range(n):

        a[i], b[i] = list(map(int, sys.stdin.readline().split()))



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

        a[i] += ans

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

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



    print(ans)





if __name__ == '__main__':

    main()
import sys





def main() -> None:

    n = int(eval(input()))

    a = [0] * n

    b = [0] * n

    ans = 0



    for i in range(n):

        a[i], b[i] = list(map(int, sys.stdin.readline().split()))



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

        a[i] += ans

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

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



    print(ans)





if __name__ == '__main__':

    main()
import sys


def main() -> None:
    n = int(sys.stdin.readline())
    a = [0] * n
    b = [0] * n
    ans = 0
    for i in range(n):
        a[i], b[i] = list(map(int, sys.stdin.readline().split()))
    for i in range(n - 1, -1, -1):
        a[i] += ans
        if a[i] % b[i] != 0:
            ans += abs(a[i] % b[i] - b[i])
    print(ans)


if __name__ == "__main__":
    main()

import sys


def main() -> None:
    n = int(eval(input()))
    a = [0] * n
    b = [0] * n
    ans = 0
    for i in range(n):
        a[i], b[i] = list(map(int, sys.stdin.readline().split()))
    for i in range(n - 1, -1, -1):
        a[i] += ans
        if a[i] % b[i] != 0:
            ans += abs(a[i] % b[i] - b[i])
    print(ans)


if __name__ == "__main__":
    main()

-    n = int(sys.stdin.readline()) +    n = int(eval(input()))