N = int(eval(input()))

AB = [[int(i) for i in input().split()] for j in range(N)]



ans = 0

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

    Atemp = AB[i][0] + ans

    ans += (0 if Atemp%AB[i][1] == 0 else AB[i][1]-Atemp%AB[i][1])

print(ans)
import sys



def solve():

    input  = sys.stdin.readline

    N = int(eval(input()))

    array = [[int(a) for a in input().split()] for _ in range(N)]

    count = 0

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

        a, b = array[i]

        if (a + count) % b == 0: continue

        else: count += b - ((a + count) % b)

    print(count)



    return 0



if __name__ == "__main__":

    solve()
N = int(eval(input()))
AB = [[int(i) for i in input().split()] for j in range(N)]
ans = 0
for i in reversed(list(range(N))):
    Atemp = AB[i][0] + ans
    ans += 0 if Atemp % AB[i][1] == 0 else AB[i][1] - Atemp % AB[i][1]
print(ans)

import sys


def solve():
    input = sys.stdin.readline
    N = int(eval(input()))
    array = [[int(a) for a in input().split()] for _ in range(N)]
    count = 0
    for i in reversed(list(range(N))):
        a, b = array[i]
        if (a + count) % b == 0:
            continue
        else:
            count += b - ((a + count) % b)
    print(count)
    return 0


if __name__ == "__main__":
    solve()

-N = int(eval(input())) -AB = [[int(i) for i in input().split()] for j in range(N)] -ans = 0 -for i in reversed(list(range(N))): -    Atemp = AB[i][0] + ans -    ans += 0 if Atemp % AB[i][1] == 0 else AB[i][1] - Atemp % AB[i][1] -print(ans) +import sys + + +def solve(): +    input = sys.stdin.readline +    N = int(eval(input())) +    array = [[int(a) for a in input().split()] for _ in range(N)] +    count = 0 +    for i in reversed(list(range(N))): +        a, b = array[i] +        if (a + count) % b == 0: +            continue +        else: +            count += b - ((a + count) % b) +    print(count) +    return 0 + + +if __name__ == "__main__": +    solve()