CodeEff/ECCO huggingface dataset:
# Original solution
import math

N=int(eval(input()))
A=[]
B=[]
for _ in range(N):
    a,b=list(map(int,input().split()))
    A.append(a)
    B.append(b)

A.reverse()
B.reverse()
#print("initial A B")
#print(A)
#print(B)
cnt=0
for i in range(N):
    tmp=math.ceil(A[i]/B[i])*B[i]-A[i]
    for j in range(i,N):
        A[j]+=tmp
    cnt+=tmp
    #print("tmp,cnt,A",tmp,cnt,A)
print(cnt)

# Optimized solution
import math

N=int(eval(input()))
A=[]
B=[]
for _ in range(N):
    a,b=list(map(int,input().split()))
    A.append(a)
    B.append(b)

A.reverse()
B.reverse()
cnt=0
for i in range(N):
    A[i]+=cnt
    tmp=math.ceil(A[i]/B[i])*B[i]-A[i]
    cnt+=tmp
print(cnt)
# Original solution
n = int(eval(input()))
x = [0]*n
y = [0]*n
for i in range(n-1, -1, -1):
  a,b = list(map(int, input().split()))
  x[i] = a
  y[i] = b

ans = 0
for i in range(n):
  while (x[i]+ans) % y[i] != 0:
    ans += 1
print(ans)

# Optimized solution
n = int(eval(input()))
x = [0]*n
y = [0]*n
for i in range(n-1, -1, -1):
  a,b = list(map(int, input().split()))
  x[i] = a
  y[i] = b

ans = 0
for i in range(n):
  if (x[i]+ans) % y[i] != 0:
    ans += y[i]-(x[i]+ans)%y[i]
print(ans)
# Original solution
n = int(eval(input()))
a_li = []
b_li = []
for _ in range(n):
    a,b = list(map(int, input().split()))
    a_li.append(a)
    b_li.append(b)

cnt = 0
for i in range(1,n+1):
    if (a_li[-i]+cnt) % b_li[-i] != 0:
        cnt += b_li[-i] - ((a_li[-i]+cnt) % b_li[-i])
print(cnt)


# Optimized solution
n = int(eval(input()))
cnt = 0
ab_l = [list(map(int, input().split())) for _ in range(n)]
for ab in ab_l[::-1]:
    a, b = ab[0], ab[1]
    a += cnt
    if a % b != 0:
        cnt += b - a%b
print(cnt)
# Original solution
_, *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)

# Optimized solution
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()
# Original solution
n=int(input())
ab=[list(map(int,input().split())) for _ in range(n)]
cnt=0
for i in range(n-1,-1,-1):
    a,b=ab[i][0],ab[i][1]
    if (a+cnt)%b==0:continue
    cnt+=b-(a+cnt)%b
print(cnt)

# Optimized solution
n=int(input())
a=[]
b=[]
for i in range(n):
    A,B=list(map(int,input().split()))
    a.append(A)
    b.append(B)
ans=0
for i in range(n-1,-1,-1):
    if (a[i]+ans)%b[i]!=0:
        ans+=b[i]-(a[i]+ans)%b[i]
print(ans)
# Original solution
# AGC009a
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # map(int, input().split())
    n = int(eval(input()))

    a = list()
    b = list()
    for _ in range(n):
        aa, bb = list(map(int, input().split()))
        a.append(aa)
        b.append(bb)

    ans = 0
    dif = 0
    for i in range(n-1, -1, -1):
        while True:
            if (a[i]+dif) % b[i] == 0:
                break
            ans += 1
            dif += 1
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
# AGC009a
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # map(int, input().split())
    n = int(eval(input()))

    a = list()
    b = list()
    for _ in range(n):
        aa, bb = list(map(int, input().split()))
        a.append(aa)
        b.append(bb)

    ans = 0
    dif = 0
    for i in range(n-1, -1, -1):
        osu = b[i]-(a[i]+dif) % b[i] if (a[i]+dif) % b[i] != 0 else 0
        #print(a[i]+dif, b[i], osu, ans)
        ans += osu
        dif += osu
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
n = int(eval(input()))
a = []
b = [] 
for i in range(n):
    x,y = list(map(int,input().split()))
    a.append(x)
    b.append(y)

res = 0

for i in range(n):
    a[n-1-i] += res
    c = a[n-1-i]%b[n-1-i]
    if c != 0:
        res += b[n-1-i]-c

print(res)

# Optimized solution
import sys
input = sys.stdin.readline


n = int(eval(input()))

a = []
b = []

for i in range(n):
    aa,bb = list(map(int,input().split()))
    a.append(aa)
    b.append(bb)

s = 0
res = 0
for i in range(n-1,-1,-1):
    a[i] += res

    if a[i]%b[i] == 0:
        continue
    
    res += b[i]-(a[i]%b[i])
print(res)
# Original solution
n = int(eval(input()))
list_a = []
list_b = []
su = 0
for i in range(n):
  a,b = list(map(int,input().split()))
  list_a.append(a)
  list_b.append(b)
list_a = list_a[::-1]
list_b = list_b[::-1]
for i in range(n):
  if list_a[i]%list_b[i]==0:
    continue
  else:
    x = list_b[i]-(list_a[i]%list_b[i])
    su += x
    for j in range(i,n):
      list_a[j] += x
 # print(list_a)
print(su)


# Optimized solution
n = int(eval(input()))
list_a = []
list_b = []
su = 0
for i in range(n):
  a,b = list(map(int,input().split()))
  list_a.append(a)
  list_b.append(b)
list_a = list_a[::-1]
list_b = list_b[::-1]
for i in range(n):
  list_a[i] += su
  if list_a[i]%list_b[i]==0:
    continue
  else:
    x = list_b[i]-(list_a[i]%list_b[i])
    su += x
#  print(list_a)
print(su)
# Original solution
N = int(eval(input()))
A = [0 for _ in range(N)]
B = [0 for _ in range(N)]
for i in range(N):
    a, b = list(map(int, input().split()))
    A[i] = a
    B[i] = b

A = A[::-1]
B = B[::-1]
R = [0 for _ in range(N)]
for i, (a, b) in enumerate(zip(A, B)):
    r = a % b
    if r > 0:
        r = b - r
    R[i] = r

ans = 0
for i in range(0, N):
    r = R[i]
    ans += r
    for j in range(i + 1, N):
        rj = R[j]
        rj -= r
        R[j] = rj % B[j]

print(ans)


# Optimized solution
N = int(eval(input()))
A = [0 for _ in range(N)]
B = [0 for _ in range(N)]
for i in range(N):
    a, b = list(map(int, input().split()))
    A[i] = a
    B[i] = b

A = A[::-1]
B = B[::-1]
R = [0 for _ in range(N)]
for i, (a, b) in enumerate(zip(A, B)):
    r = a % b
    if r > 0:
        r = b - r
    R[i] = r

ans = R[0]
for i in range(1, N):
    r = R[i]
    r = r - ans
    r = r % B[i]
    ans += r

print(ans)
# Original solution
# coding: utf-8
# Your code here!
kai = int(eval(input()))
a = 0
b = 0
cou = 0
kaz = []

for i in range(kai):
    a,b = list(map(int,input().split(" ")))
    kaz.append(a)
    kaz.append(b)

for j in range(kai):
    while True:
        if (kaz[(kai-j)*2 -2]+cou) % kaz [(kai-j)*2 -1] == 0:
            break
        else:
            cou += 1
print(cou)

# Optimized solution
n = int(eval(input()))
p = []
for i in range(n):
    a , b = list(map(int,input().split()))
    p.append([a,b])
ans = 0
for i in reversed(list(range(n))):
    if (p[i][0] + ans) % p[i][1] != 0:
        ans += p[i][1] - ((p[i][0] + ans)%p[i][1])
print(ans)
# Original solution
N = int(eval(input()))
A = [list(map(int, input().split())) for _ in range(N)]
ans = 0
for i in range(N-1, -1, -1):
    ans += (A[i][1] - (A[i][0]+ans) % A[i][1]) % A[i][1]
print(ans)


# Optimized solution
ans = 0
for A, B in [list(map(int, input().split())) for _ in range(int(eval(input())))][::-1]:
    ans += (B - A - ans) % B
print(ans)
# Original solution
n,*a=list(map(int,open(0).read().split()))
#print(a)
sumcount=0
for s in range(n*2-1,-1,-2):
  hidari=a[s-1]+sumcount
#  print(hidari,a[s],sumcount)
  if hidari%a[s]==0:
    continue
  elif hidari%a[s]!=0 and hidari//a[s]==0:
    sumcount+=a[s]-hidari
  elif hidari%a[s]!=0 and hidari//a[s]!=0:
    sumcount+=(hidari//a[s]+1)*a[s]-hidari
print(sumcount)

# Optimized solution
n,*a=list(map(int,open(0).read().split()))
sumcount=0
for s in range(n*2-1,-1,-2):
  hidari=a[s-1]+sumcount
  if hidari%a[s]!=0 and hidari//a[s]==0:
    sumcount+=a[s]-hidari
  if hidari%a[s]!=0 and hidari//a[s]!=0:
    sumcount+=(hidari//a[s]+1)*a[s]-hidari
print(sumcount)
# Original solution
#!/usr/bin/env python3
n, *d = list(map(int, open(0).read().split()))
a = d[::2]
b = d[1::2]
s = 0
for i in range(n):
    t = a[-i-1] % b[-i-1]
    if t: t = b[-i-1] - t
    *a, = [x+t for x in a]
    s += t
print(s)

# Optimized solution
#!/usr/bin/env python3
_, *s = open(0)
c = 0
for t in s[::-1]:
    a, b = list(map(int, t.split()))
    c += (-a-c) % b
print(c)
# Original solution
def floor(a, b):
    return (-a)//b*(-1)


def inc_list(i, l):
    for j in range(i):
        l[j] += 1
    return l


def inc_mult(i, n, l):
    for _ in range(n):
        l = inc_list(i, l)
    return l


N = int(eval(input()))
A = []
B = []
for _ in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)


ans = 0
for i in reversed(list(range(N))):
    a = A.pop(-1)
    b = B.pop(-1)
    inc = floor(a, b)*b - a
    A = inc_mult(i, inc, A)
    ans += inc

print(ans)


# Optimized solution
def floor(a, b):
    return (-a)//b*(-1)


N = int(eval(input()))
A = []
B = []
total_add = 0
for _ in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)


ans = 0
for i in reversed(list(range(N))):
    a = A.pop(-1)
    a += total_add
    b = B.pop(-1)
    inc = b*floor(a, b)-a
    total_add += inc

print(total_add)
# Original solution
n=int(eval(input()))
ab=[list(map(int, input().split())) for _ in range(n)]

cnt=0
for i in range(n):
    a=ab[n-i-1][0]
    b=ab[n-i-1][1]
    a+=cnt
    amari=a%b
    shou=a//b
    if amari!=0:
        cnt+=b-amari
print(cnt)

# Optimized solution
n=int(eval(input()))
ab=[list(map(int, input().split())) for _ in range(n)]

cnt=0
for i in range(n):
    a=ab[n-i-1][0]
    b=ab[n-i-1][1]
    a+=cnt
    amari=a%b
    if amari!=0:
        cnt+=b-amari
print(cnt)
# Original solution
N = int(eval(input()))
A=[]
B=[]
for i in range(N):
    a,b=[int(n) for n in input().split()]
    A.append(a)
    B.append(b)

cnt=0

# print(list(reversed(range(N))))
for i in reversed(list(range(N))):
    if A[i]%B[i]==0:
        r=0
    else:
        r=B[i]-A[i]%B[i]
    cnt+=r
    A=[A[n]+r if n<=i else A[n] for n in range(N)]

print(cnt)


# Optimized solution
N = int(eval(input()))
A=[]
B=[]
for i in range(N):
    a,b=[int(n) for n in input().split()]
    A.append(a)
    B.append(b)

cnt=0

# print(list(reversed(range(N))))
for i in reversed(list(range(N))):
    if (A[i]+cnt)%B[i]==0:
        d=0
    else:
        d=B[i]-(A[i]+cnt)%B[i]
    cnt+=d
#    A=[A[n]+r if n<=i else A[n] for n in range(N)]

print(cnt)
# Original solution
N = int(eval(input()))
A = []
B = []
for i in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)

A = A[::-1]
B = B[::-1]

ans = 0
for a, b in zip(A, B):
    a += ans
    if a % b != 0: 
        m = b - (a % b) 
        ans += m
print(ans)

# Optimized solution
N = int(eval(input()))
AB = reversed([[int(j) for j in input().split()] for i in range(N)])
ans = 0
for a, b in AB:
    a += ans
    if a % b != 0: 
        ans += b - (a % b) 
print(ans)
# Original solution
N=int(eval(input()))
AB=[list(map(int,input().split())) for i in range(N)][::-1]
plus=0
for j in range(N):
    AB[j][0]+=plus
    while AB[j][0]%AB[j][1]!=0:
        AB[j][0]+=1
        plus+=1
print(plus)

# Optimized solution
N=int(eval(input()))
AB=[list(map(int,input().split())) for i in range(N)][::-1]
plus=0
for j in range(N):
    AB[j][0]+=plus
    if AB[j][0]%AB[j][1]!=0:
        a=AB[j][1]-(AB[j][0]%AB[j][1])
        AB[j][0]+=a
        plus+=a
print(plus)
# Original solution
N = int(eval(input()))
A = []
B = []
for i in range(N):
    a,b = list(map(int,input().split()))
    A.append(a)
    B.append(b)
k = 0 
for i in range(N-1,-1,-1):
    if A[i] % B[i] == 0 :
        continue
    else :
        c = (B[i] - A[i] % B[i])
        k += c
        for j in range(i):
            A[j] += c

print(k)

# Optimized solution
N = int(eval(input()))
A = []
B = []
for i in range(N):
    a,b = list(map(int,input().split()))
    A.append(a)
    B.append(b)
k = 0 
for i in range(N-1,-1,-1):
    A[i] += k
    if A[i] % B[i] == 0 :
        pass
    else :
        c = (B[i] - A[i] % B[i])
        k += c

print(k)
# Original solution
n=int(eval(input()))
A=[]
B=[]
for i in range(n):
    a,b=input().split()
    A.append(a)
    B.append(b)
A.reverse()
B.reverse()
A.append(0)
B.append(0)
A=list(map(int,A))
B=list(map(int,B))

v=[0]
for i in range(0,n):
    cnt=0
    if A[i]%B[i]!=0:
        cnt+=(B[i]-A[i]%B[i])
        A[i]+=(B[i]-A[i]%B[i])
    v.append(cnt)
    A[i+1]+= sum(v)
print((sum(v)))



# Optimized solution
n=int(eval(input()))
A=[]
B=[]
for i in range(n):
    a,b=input().split()
    A.append(a)
    B.append(b)
A.reverse()
B.reverse()
A.append(0)
B.append(0)
A=list(map(int,A))
B=list(map(int,B))

x=0
for i in range(0,n):
    cnt=0
    if A[i]%B[i]!=0:
        cnt+=(B[i]-A[i]%B[i])
        A[i]+=(B[i]-A[i]%B[i])
    x+=cnt
    A[i+1]+= x
print(x)
# Original solution
n = int(eval(input()))
a_input = [0]*n
b_input = [0]*n
for i in range(n):
    a_input[i],b_input[i]=list(map(int,input().split()))

ans=0
for i in range(n-1,-1,-1):
    if a_input[i]%b_input[i]==0:
        continue
    tmp = b_input[i]-a_input[i]%b_input[i]
    ans += tmp
    for j in range(i+1):
        a_input[j]+=tmp
print(ans)


# Optimized solution
n = int(eval(input()))
a_input = [0]*n
b_input = [0]*n
for i in range(n):
    a_input[i],b_input[i]=list(map(int,input().split()))

a_input=a_input[::-1]
b_input=b_input[::-1]

ans=0
for i in range(n):
    a = a_input[i]+ans
    b = b_input[i]
    tmp = a%b
    if tmp==0:
        continue
    ans+=(a//b+1)*b-a
print(ans)
# Original solution
N = int(eval(input()))
A = []
B = []
for i in range(N):
    a,b = list(map(int,input().split()))
    A.append(a)
    B.append(b)
count2 = 0
for i in range(N-1,-1,-1):
    count = B[i]*((A[i]-1)//B[i] + 1) - A[i]
    count2 += count
    for j in range(i+1):
        A[j] += count
print(count2)


# Optimized solution
N = int(eval(input()))
A = []
B = []
for i in range(N):
    a,b = list(map(int,input().split()))
    A.append(a)
    B.append(b)
count2 = 0
for i in range(N-1,-1,-1):
    count = B[i]*((A[i]+count2-1)//B[i] + 1) - A[i]-count2
    count2 += count
print(count2)
# Original solution
n = int(eval(input()))
l = []
for i in range(n):
    i = list(map(int,input().split()))
    l.append(i)
tmp = 0

for j in reversed(list(range(n))):
    l[j][0] += tmp
    while  l[j][0]%l[j][1] != 0:
        l[j][0] += 1
        tmp += 1

print(tmp)

# Optimized solution
n = int(eval(input()))
l = []
for i in range(n):
    i = list(map(int,input().split()))
    l.append(i)
tmp = 0

for j in reversed(list(range(n))):
    l[j][0] += tmp
    if l[j][0]%l[j][1] != 0:
        tmp += l[j][1] - l[j][0]%l[j][1]

print(tmp)
# Original solution
n = int(eval(input()))

a, b = [], []
for _ in range(n):
    ta, tb = list(map(int, input().split()))
    a.append(ta)
    b.append(tb)

ans = 0
i = n - 1
while i >= 0:
    r = a[i] % b[i]
    if r != 0:
        t = b[i] - r
        ans += t
        for j in range(i):
            a[j] += t
    i -= 1

print(ans)

# Optimized solution
n = int(eval(input()))

a, b = [], []
for _ in range(n):
    ta, tb = list(map(int, input().split()))
    a.append(ta)
    b.append(tb)

ans = 0
i = n - 1
while i >= 0:
    a[i] += ans
    r = a[i] % b[i]
    if r != 0:
        ans += b[i] - r
    i -= 1

print(ans)
# Original solution
n = int(eval(input()))

a, b = [], []
for _ in range(n):
    ta, tb = list(map(int, input().split()))
    a.append(ta)
    b.append(tb)

ans = 0
i = n - 1
while i >= 0:
    a[i] += ans
    r = a[i] % b[i]
    if r != 0:
        ans += b[i] - r
    i -= 1

print(ans)

# Optimized solution
N = int(eval(input()))
a, b = [], []
for _ in range(N):
    aa, bb = list(map(int, input().split()))
    a.append(aa)
    b.append(bb)

ans = 0
add_v = 0
i = N - 1
while i >= 0:
    r = (a[i] + add_v) % b[i]
    if r != 0:
        to_add = b[i] - r
        add_v += to_add
        ans += to_add
    i -= 1
print(ans)
# Original solution
N=int(eval(input()))
Z=[list(map(int,input().split())) for i in range(N)]
A,B=list(zip(*Z))
A=list(A[::-1])
B=list(B[::-1])
ans=0
for i in range(N):
  A[i]+=ans
  while A[i]%B[i]!=0:
    ans+=1
    A[i]+=1
print(ans)

# Optimized solution
N=int(eval(input()))
Z=[list(map(int,input().split())) for i in range(N)]
A,B=list(zip(*Z))
A=list(A[::-1])
B=list(B[::-1])
ans=0
for i in range(N):
  A[i]+=ans
  if A[i]%B[i]!=0:
    a=B[i]-(A[i]%B[i])
    ans+=a
print(ans)
# Original solution
n = int(eval(input()))
cnt = 0
a = [0] * n
b = [0] * n
for i in range(n):
    a[i], b[i] = list(map(int, input().split()))

for i in range(n - 1, -1, -1):
    if a[i] % b[i] != 0:
        r = b[i] - a[i] % b[i]
        cnt += r
        for j in range(i + 1):
            a[j] += r

print(cnt)


# Optimized solution
n = int(eval(input()))
cnt = 0
a = [0] * n
b = [0] * n
for i in range(n):
    a[i], b[i] = list(map(int, input().split()))

for i in range(n - 1, -1, -1):
    r = b[i] - (a[i] + cnt) % b[i]
    if  r != b[i]:
        cnt += r
print(cnt)
# Original solution
N=int(eval(input()))
AB=[list(map(int,input().split())) for i in range(N)][::-1]
count=0
for i in range(N):
    AB[i][0]+=count
    if AB[i][0]%AB[i][1]!=0:
        count+=AB[i][1]-(AB[i][0]%AB[i][1])
print(count)

# Optimized solution
N=int(eval(input()))
AB=[list(map(int,input().split())) for i in range(N)]
count=0
for a,b in AB[::-1]:
    a+=count
    if a%b!=0:
        count+=b-(a%b)
print(count)
# Original solution
"""
Nは10**5以下。O(N)なら通る
"""

N = int(eval(input()))
A = []
B = []
for i in range(N):
    s = list(map(int, input().split()))
    A.append(s[0])
    B.append(s[1])

count = 0
for i in reversed(list(range(N))):
    if A[i] % B[i] == 0:
        continue
    else:
        s = B[i] - (A[i] % B[i])
        A[:i+1] = [x + s for x in A[:i+1]]
        count += s

print(count)

# Optimized solution
"""
Nは10**5以下。O(N)なら通る
"""

N = int(eval(input()))
A = []
B = []
for i in range(N):
    s = list(map(int, input().split()))
    A.append(s[0])
    B.append(s[1])

count = 0
for i in reversed(list(range(N))):
    if (count + A[i]) % B[i] == 0:
        continue
    else:
        s = B[i] - (count + A[i]) % B[i]
        count += s

print(count)
# Original solution
N = int(eval(input()))

A = [None] * N
B = [None] * N

for i in range(1, N+1):
    a, b = list(map(int, input().split()))
    A[N-i] = a
    B[N-i] = b

cnt = 0
for i in range(N):
    A_add = A[i] + cnt
    r =  A_add % B[i]
    if r == 0:
        pass
    else:
        cnt += B[i] - r
print(cnt)


# Optimized solution
import sys
N = int(eval(input()))

A = [None] * N
B = [None] * N

for i in range(1, N+1):
    a, b = list(map(int, sys.stdin.readline().split()))
    A[N-i] = a
    B[N-i] = b

cnt = 0
for i in range(N):
    A_add = A[i] + cnt
    r =  A_add % B[i]
    if r == 0:
        pass
    else:
        cnt += B[i] - r
print(cnt)
# Original solution
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)

# Optimized solution
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()
# Original solution
N = int(eval(input()))
A = [list(map(int,input().split())) for k in range(N)]

ans = 0
for k in range(N):
    A[N-1-k][0] += ans
    while A[N-1-k][0]%A[N-1-k][1]!=0:
        A[N-1-k][0] += 1
        ans += 1

print(ans)

# Optimized solution
N = int(eval(input()))
A = [list(map(int,input().split())) for k in range(N)]

ans = 0
for k in range(N):
    A[N-1-k][0] += ans
    if A[N-1-k][0]%A[N-1-k][1]!=0:
        d = A[N-1-k][1]*(1+A[N-1-k][0]//A[N-1-k][1])-A[N-1-k][0]
        ans += d
        A[N-1-k][0] += d

print(ans)
# Original solution
# -*- coding: utf-8 -*-
n = int(eval(input()))
ab = []
for _ in range(n):
    a,b = list(map(int, input().split()))
    ab.append((a,b))

res = 0
for i in range(n-1,-1,-1):
    a,b = ab[i]
    a += res
    if a%b==0:
        pass
    elif b>=a:
        res += b-a
    else:
        res += b-a%b
print(res)


# Optimized solution
# -*- coding: utf-8 -*-
n = int(eval(input()))
ab = []
for _ in range(n):
    a,b = list(map(int, input().split()))
    ab.append((a,b))

res = 0
for i in range(n-1,-1,-1):
    a,b = ab[i]
    a += res
    if b>=a>0:
        res += b-a
    elif a%b==0:
        pass
    else:
        res += b-a%b
print(res)
# Original solution
n=int(eval(input()))
a=[0]*n;b=a[:]
for i in range(n):
  c,d=list(map(int,input().split()))
  a[i]=c;b[i]=d
ans=0
for i in range(n)[::-1]:
  a[i]+=ans
  while a[i]%b[i]!=0:
    a[i]+=1
    ans+=1
print(ans)


# Optimized solution
n=int(eval(input()))
a=[0]*n;b=a[:]
for i in range(n):
  c,d=list(map(int,input().split()))
  a[i]=c;b[i]=d
ans=0
for i in range(n)[::-1]:
  a[i]+=ans
  if a[i]%b[i]==0:continue
  ans+=b[i]-(a[i]%b[i])
print(ans)
# Original solution
n = int(eval(input()))
A = []
B = []
for i in range(n):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)

ans = 0
for i in range(n)[-1::-1]:
    val = B[i] - A[i]%B[i]
    if val == B[i]:
        continue
    ans += val
    j = 0
    while j != i:
        A[j] += val
        j += 1
print(ans)

# Optimized solution
n = int(eval(input()))
A = []
B = []
for i in range(n):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)

ans = 0
for i in range(n)[-1::-1]:
    val = B[i] - (A[i] + ans)%B[i]
    if val == B[i]:
        continue
    ans += val
    
print(ans)
# Original solution
n = int(eval(input()))
a,b=[],[]

for i in range(n):
    x,y = list(map(int,input().split()))
    a.append(x)
    b.append(y)

a=list(reversed(a))
b=list(reversed(b))

OK=0
for i in range(n):
    A=a[i]+OK
    if A<b[i]:
        ok=b[i]-A
    if A==b[i]:
        continue
    else:
        ok=-(-A//b[i])*b[i]-A
    OK+=ok

print(OK)


# Optimized solution
n = int(eval(input()))
a,b=[],[]

for i in range(n):
    x,y = list(map(int,input().split()))
    a.append(x)
    b.append(y)

a=list(reversed(a))
b=list(reversed(b))

OK=0
for i in range(n):
    A=a[i]+OK
    if A==b[i]:
        continue
    else:
        ok=-(-A//b[i])*b[i]-A
    OK+=ok

print(OK)
# Original solution
n = int(eval(input()))
ab = [list(map(int, input().split())) for i in range(n)][::-1]

ans = 0
for a, b in ab:
    tmp = a
    while tmp % b != 0:
        tmp += 1
    for i in range(n):
        ab[i][0] += tmp - a
    ans += tmp - a
print(ans)

# Optimized solution
n = int(eval(input()))
ab = [list(map(int, input().split())) for i in range(n)][::-1]

ans = 0
for a, b in ab:
    a += ans
    # 負の剰余。
    ans += -a % b
print(ans)
# Original solution
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    ab = [list(map(int, (str(i) + " " + input().rstrip('\n')).split())) for i in range(n)]
    ab.sort(reverse=True)
    cnt = 0
    for i, a, b in ab:
        if (a + cnt) % b != 0:
            cnt += (b - (a + cnt) % b)
    print(cnt)


if __name__ == '__main__':
    slove()


# Optimized solution
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    t = 0
    for i in range(n-1, -1, -1):
        while True:
            if (ab[i][0] + t) % ab[i][1] == 0:
                break
            else:
                t += (ab[i][1] - (ab[i][0] + t) % ab[i][1])
    print(t)


if __name__ == '__main__':
    slove()
# Original solution
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    t = 0
    for i in range(n-1, -1, -1):
        while True:
            if (ab[i][0] + t) % ab[i][1] == 0:
                break
            else:
                t += (ab[i][1] - (ab[i][0] + t) % ab[i][1])
    print(t)


if __name__ == '__main__':
    slove()


# Optimized solution
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    t = 0
    for i in range(n):
        a, b = ab[-i-1]
        a += t
        if a % b != 0:
            t += ((a + b - 1) // b * b - a)
    print(t)


if __name__ == '__main__':
    slove()