output_description: Print an integer representing the minimum number of times Takahashi will press
  the buttons.
  
  * * *
input_description: The input is given from Standard Input in the following format:
  
      
      
      N
      A_1 B_1
      :
      A_N B_N
attempt: # 1st: ARC007-B
  if False:
      N, M = map(int, input().split())
      case = [i for i in range(N + 1)]
      for _ in range(M):
          now = int(input())
          i = case.index(now)
          case[i] = case[0]
          case[0] = now
          # print(case)
      for i in range(1, N + 1):
          print(case[i])
  
  # 2nd: ABC004-C (0408)
  if False:
      N = int(input()) - 1
      N = N % 30
      ans = list(range(1, 7))
      for i in range(N + 1):
          ans[i % 5], ans[i % 5 + 1] = ans[i % 5 + 1], ans[i % 5]
          # print(ans)
      print("".join(map(str, ans)))
  
  # 3rd: AGC011-A (0408)
  if False:
      N, C, K = map(int, input().split())
      T = [int(input()) for _ in range(N)]
      T.sort()
      cnt = 1
      pas = 1  # 乗客数
      tim = K + T[0]  # departure time
      # print(T)
      for i in range(1, N):
          t = T[i]
          if tim < t or pas >= C:
              cnt += 1
              tim = t + K
              pas = 1
          else:
              pas += 1
          # print(tim,pas,cnt)
      print(cnt)
  
  # 4th: ABC072-D (0408)
  if False:
      N = int(input())
      P = [int(i) for i in input().split()]
      ans = 0
      cnt = 0
      for i in range(N - 1):
          if P[i] == i + 1:
              P[i], P[i + 1] = P[i + 1], P[i]
              cnt += 1
      if P[N - 1] == N:
          P[N - 1], P[N - 2] = P[N - 2], P[N - 1]
          cnt += 1
      print(cnt)
  
  # 5th: AGC009-A (0408)
  if True:
      N = int(input())
      A = []
      B = []
      for _ in range(N):
          a, b = map(int, input().split())
          A.append(a)
          B.append(b)
      cnt = 0
      for i in reversed(range(N)):
          if (A[i] + cnt) % B[i] != 0:
              cnt += B[i] - (A[i] + cnt) % B[i]
          # print(cnt)
      print(cnt)
  
problem_description: Statement
  
  There are an integer sequence A_1,...,A_N consisting of N terms, and N
  buttons. When the i-th (1 ≦ i ≦ N) button is pressed, the values of the i
  terms from the first through the i-th are all incremented by 1.
  
  There is also another integer sequence B_1,...,B_N. Takahashi will push the
  buttons some number of times so that for every i, A_i will be a multiple of
  B_i.
  
  Find the minimum number of times Takahashi will press the buttons.
samples: [{"input": "3\n    3 5\n    2 7\n    9 4", "output": "7\n    \n\nPress the first button twice, the second button twice and the third button\nthree times.\n\n* * *"}, {"input": "7\n    3 1\n    4 1\n    5 9\n    2 6\n    5 3\n    5 8\n    9 7", "output": "22"}]