from math import ceil





def main():

    length = int(eval(input()))

    sequence = [0 for _ in range(length)]

    target = [0 for _ in range(length)]

    for i in range(length):

        a, b = list(map(int, input().split()))

        sequence[i] = a

        target[i] = b

    answer = 0

    cumulative_push = 0

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

        sequence[i] += cumulative_push

        now_push = ceil(sequence[i] / target[i]) * target[i] - sequence[i]

        cumulative_push += now_push

        answer += now_push

    print(answer)





if __name__ == '__main__':

    main()



from math import ceil





def main():

    n = int(eval(input()))

    a = [0 for _ in range(n)]

    b = [0 for _ in range(n)]

    for i in range(n):

        a[i], b[i] = list(map(int, input().split()))

    added = 0

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

        added += ceil((a[i] + added) / b[i]) * b[i] - (a[i] + added)

    print(added)





if __name__ == '__main__':

    main()



from math import ceil


def main():
    length = int(eval(input()))
    sequence = [0 for _ in range(length)]
    target = [0 for _ in range(length)]
    for i in range(length):
        a, b = list(map(int, input().split()))
        sequence[i] = a
        target[i] = b
    answer = 0
    cumulative_push = 0
    for i in range(-1, -length - 1, -1):
        sequence[i] += cumulative_push
        now_push = ceil(sequence[i] / target[i]) * target[i] - sequence[i]
        cumulative_push += now_push
        answer += now_push
    print(answer)


if __name__ == "__main__":
    main()

from math import ceil


def main():
    n = int(eval(input()))
    a = [0 for _ in range(n)]
    b = [0 for _ in range(n)]
    for i in range(n):
        a[i], b[i] = list(map(int, input().split()))
    added = 0
    for i in range(n - 1, -1, -1):
        added += ceil((a[i] + added) / b[i]) * b[i] - (a[i] + added)
    print(added)


if __name__ == "__main__":
    main()

-    length = int(eval(input())) -    sequence = [0 for _ in range(length)] -    target = [0 for _ in range(length)] -    for i in range(length): -        a, b = list(map(int, input().split())) -        sequence[i] = a -        target[i] = b -    answer = 0 -    cumulative_push = 0 -    for i in range(-1, -length - 1, -1): -        sequence[i] += cumulative_push -        now_push = ceil(sequence[i] / target[i]) * target[i] - sequence[i] -        cumulative_push += now_push -        answer += now_push -    print(answer) +    n = int(eval(input())) +    a = [0 for _ in range(n)] +    b = [0 for _ in range(n)] +    for i in range(n): +        a[i], b[i] = list(map(int, input().split())) +    added = 0 +    for i in range(n - 1, -1, -1): +        added += ceil((a[i] + added) / b[i]) * b[i] - (a[i] + added) +    print(added)