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



AB = iter(AB[::-1])

increment = 0



for b, a in zip(AB, AB):

  a += increment

  if a % b:

    increment += b - a % b

    

print(increment)
def main():

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



  AB = iter(AB[::-1])

  increment = 0



  for b, a in zip(AB, AB):

    a += increment

    if a % b:

      increment += b - a % b



  print(increment)

  

if __name__=="__main__":

  main()
_, *AB = list(map(int, open(0).read().split()))
AB = iter(AB[::-1])
increment = 0
for b, a in zip(AB, AB):
    a += increment
    if a % b:
        increment += b - a % b
print(increment)

def main():
    _, *AB = list(map(int, open(0).read().split()))
    AB = iter(AB[::-1])
    increment = 0
    for b, a in zip(AB, AB):
        a += increment
        if a % b:
            increment += b - a % b
    print(increment)


if __name__ == "__main__":
    main()

-_, *AB = list(map(int, open(0).read().split())) -AB = iter(AB[::-1]) -increment = 0 -for b, a in zip(AB, AB): -    a += increment -    if a % b: -        increment += b - a % b -print(increment) +def main(): +    _, *AB = list(map(int, open(0).read().split())) +    AB = iter(AB[::-1]) +    increment = 0 +    for b, a in zip(AB, AB): +        a += increment +        if a % b: +            increment += b - a % b +    print(increment) + + +if __name__ == "__main__": +    main()