# AGC009A - Multiple Array

# greedy algorithm

import sys

input = sys.stdin.readline



def main():

    n = int(eval(input()))

    A = tuple(tuple(map(int, input().split())) for _ in range(n))

    ans = 0

    for i, j in A[::-1]:  # # decide from the last one

        ans += -(i + ans) % j

    print(ans)





if __name__ == "__main__":

    main()
# AGC009A - Multiple Array

def main():

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

    ans = 0

    for i, j in zip(*[iter(AB[::-1])] * 2):  # decide from the last one greedily

        ans += -(j + ans) % i

    print(ans)





if __name__ == "__main__":

    main()
# AGC009A - Multiple Array
# greedy algorithm
import sys

input = sys.stdin.readline


def main():
    n = int(eval(input()))
    A = tuple(tuple(map(int, input().split())) for _ in range(n))
    ans = 0
    for i, j in A[::-1]:  # # decide from the last one
        ans += -(i + ans) % j
    print(ans)


if __name__ == "__main__":
    main()

# AGC009A - Multiple Array
def main():
    N, *AB = list(map(int, open(0).read().split()))
    ans = 0
    for i, j in zip(*[iter(AB[::-1])] * 2):  # decide from the last one greedily
        ans += -(j + ans) % i
    print(ans)


if __name__ == "__main__":
    main()

-# greedy algorithm -import sys - -input = sys.stdin.readline - - -    n = int(eval(input())) -    A = tuple(tuple(map(int, input().split())) for _ in range(n)) +    N, *AB = list(map(int, open(0).read().split())) -    for i, j in A[::-1]:  # # decide from the last one -        ans += -(i + ans) % j +    for i, j in zip(*[iter(AB[::-1])] * 2):  # decide from the last one greedily +        ans += -(j + ans) % i