import sys



sys.setrecursionlimit(10 ** 7)

f_inf = float('inf')

mod = 10 ** 9 + 7





def resolve():

    n = int(eval(input()))

    AB = [list(map(int, input().split())) for _ in range(n)][::-1]



    res = 0

    for i in range(n):

        a, b = AB[i]

        push = ((a + res + b - 1) // b) * b - a

        res = push

    print(res)





if __name__ == '__main__':

    resolve()

import sys



sys.setrecursionlimit(10 ** 7)

input = sys.stdin.readline

f_inf = float('inf')

mod = 10 ** 9 + 7





def resolve():

    n = int(eval(input()))

    AB = [list(map(int, input().split())) for _ in range(n)]



    res = 0

    for i in reversed(list(range(n))):

        a, b = AB[i]

        a += res

        if a % b != 0:

            res += (a // b + 1) * b - a

    print(res)





if __name__ == '__main__':

    resolve()

import sys

sys.setrecursionlimit(10**7)
f_inf = float("inf")
mod = 10**9 + 7


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


if __name__ == "__main__":
    resolve()

import sys

sys.setrecursionlimit(10**7)
input = sys.stdin.readline
f_inf = float("inf")
mod = 10**9 + 7


def resolve():
    n = int(eval(input()))
    AB = [list(map(int, input().split())) for _ in range(n)]
    res = 0
    for i in reversed(list(range(n))):
        a, b = AB[i]
        a += res
        if a % b != 0:
            res += (a // b + 1) * b - a
    print(res)


if __name__ == "__main__":
    resolve()

+input = sys.stdin.readline -    AB = [list(map(int, input().split())) for _ in range(n)][::-1] +    AB = [list(map(int, input().split())) for _ in range(n)] -    for i in range(n): +    for i in reversed(list(range(n))): -        push = ((a + res + b - 1) // b) * b - a -        res = push +        a += res +        if a % b != 0: +            res += (a // b + 1) * b - a