qyliang/CodeNanoFix huggingface dataset:
# Buggy code:
a = int(input())
b = input().split()
x = []
for i in b:
    x.append(int(i))

count = 0

for i in x:
    if i % 3 == 0:
        count += 1

if count % 2 == 0:
    print("YES")
else:
    print("NO")  

# Fixed code:

a = int(input())
b = input().split()
x = []
for i in b:
    x.append(int(i))

count = 0

for i in x:
    if i % 2 != 0:
        count += 1


z = count % 2

if z == 1:
    print("NO")
else:
    print("YES")
# Buggy code:
n = int(input())
a = list(map(int,input().split()))
odd = 0
for i in range(n):
    if a[i]%2==1:
        odd+=1
if odd%2==0:
    print('YES')
else:
    pritn('NO')

# Fixed code:
n = int(input())
odd = 0
a = list(map(int, input().split()))
for i in range(n):
    if a[i] % 2:
        odd += 1
if odd % 2:
    print('NO')
else:
    print('YES')
# Buggy code:
N = int(input())
A = [int(n) for n in input().split()]

cnt = 0
for in range(N):
    if A[i] % 2 == 1:
        cnt += 1

if cnt % 2 == 0:
    print ('YES')
else:
    print('NO')


# Fixed code:
N = int(input())
A = [int(n) for n in input().split()]

cnt = 0
for i in range(N):
    if A[i] % 2 == 1:
        cnt += 1

if cnt % 2 == 0:
    print ('YES')
else:
    print('NO')
# Buggy code:
N = int(input())
A = list(map(int, input().split()))

cnt = 0 
for x in A:
  if A%2 == 1:
    cnt += 1
    
if cnt % 2 == 0:
  print("YES")
else:
  print("NO")

# Fixed code:
N = int(input())
A = list(map(int, input().split()))

cnt = 0 
for x in A:
  if x%2 == 1:
    cnt += 1
    
if cnt % 2 == 0:
  print("YES")
else:
  print("NO")
# Buggy code:
input()
list_ = [n % 2 for n in map(int, input().rstrip("\n").split())]

print("YES" if list_.count(1) % 2 else "NO")

# Fixed code:
input()
list_ = [n % 2 for n in map(int, input().rstrip("\n").split())]

print("NO" if list_.count(1) % 2 else "YES")
# Buggy code:
from sys import stdin
data = [int(x) for x in stdin.readline().rstrip().split()]
cnt = 0
for i in data:
    if i % 2 != 0:
        cnt += 1
if cnt % 2 == 0:
    print("YES")
else:
    print("NO")

# Fixed code:
from sys import stdin
N = int(stdin.readline().rstrip())
data = [int(x) for x in stdin.readline().rstrip().split()]
cnt = 0
for i in data:
    if i % 2 != 0:
        cnt += 1
if cnt % 2 == 0:
    print("YES")
else:
    print("NO")
# Buggy code:
length = int(input())
target = [int(n) for n in input().split(" ")]
even,odd = 0,0
for l in range(length):
    if target[l] % 2 == 0:
        even += 1
    else:
        odd += 1
if even % 2 == 0:
    print("YES")
else:
    print("NO")

# Fixed code:
length = int(input())
target = [int(n) for n in input().split(" ")]
even,odd = 0,0
for l in range(length):
    if target[l] % 2 == 0:
        even += 1
    else:
        odd += 1
if odd % 2 == 0:
    print("YES")
else:
    print("NO")
# Buggy code:
N=int(input())
A=list(map(int,input().split()))
Even = []
Odd = []
A.sort()
for a in A:
    if a % 2 == 0:
        a.append(Even)
    if a % 2 == 1:
        a.append(Odd)
if len(Odd) % 2 == 1:
    print('NO')
else:
    print('YES')
        

# Fixed code:
N=int(input())
A=list(map(int,input().split()))
Even = []
Odd = []
A.sort()
for a in A:
    if a % 2 == 0:
        Even.append(a)
    if a % 2 == 1:
        Odd.append(a)
if len(Odd) % 2 == 1:
    print('NO')
else:
    print('YES')
# Buggy code:
print(sum([1 if i % 2 == 1 else 0 for i in [int(input()) % 1] + list(map(int, input().split(' ')))]))

# Fixed code:
print(['YES', 'NO'][sum([1 if i % 2 == 1 else 0 for i in [int(input()) % 1] + list(map(int, input().split(' ')))]) % 2])
# Buggy code:
n = int()
a = list(map(int, input().split(' ')))
sum = 0
for i in range(n):
    sum += a[i]
if sum % 2 == 0:
    print('YES')
else:
    print('NO')

# Fixed code:
n = int(input())
a = list(map(int,input().split()))
bi = [i %2 for i in a]
if sum(bi)%2 ==0:
    print("YES")
else: print("NO")
# Buggy code:
n=int(input())
a=list(map(int,input().split()))
od=int(0);ev=int(0)
for i in range(n):
    if a[i]%2==0:
        od+=1
    else:
        ev+=1
if od%2==0 and ev%2!=0:
    print('NO')
elif od%2!=0 and ev%2==0:
    print('NO')
else:
    print('YES')


# Fixed code:
n=int(input())
a=list(map(int,input().split()))
od=int(0);ev=int(0)
for i in range(n):
    if a[i]%2==0:
        od+=1
    else:
        ev+=1
if ev%2!=0:
    print('NO')
else:
    print('YES')
# Buggy code:
n=int(input())
a=list(map(int,input().split()))

cnt=0
for i in a:
  if i%2=1:
    cnt+=1
    
if cnt%2==1:
  print('NO')
  
else:
  print('YES')

# Fixed code:
n = int(input())
a = list(map(int,input().split()))
ki = 0
for i in a:
  if i%2==1:
    ki+=1
if ki%2 == 1:
  print("NO")
else:
  print("YES")
# Buggy code:
n = input()
a_list = [int(i) for i in input().split()]

add_or_not = [True if i%2==1 else False for i in a_list]
if sum(add_or_not) %2 == 1:
  print('YES')
else:
  print('NO')

# Fixed code:
n = input()
a_list = [int(i) for i in input().split()]

add_or_not = [True if i%2==1 else False for i in a_list]
if sum(add_or_not) %2 == 1:
  print('NO')
else:
  print('YES')
# Buggy code:
n=int(input())
arr=list(map(int,input().split()))
count=0
for i in range(n):
  if arr[0]%2==1:
    count+=1
if count%2==1:
  print('NO')
else:
  print('YES')

# Fixed code:
n = int(input())
a = list(map(int, input().split()))

count = 0

for i in range(n):
    if a[i] % 2 == 1:
        count += 1

if count % 2 == 1:
    print("NO")
else:
    print("YES")
# Buggy code:
import numpy as np

N = int(input())
a = input().split(" ")
A = [int(i) for i in a]
print(A)
k = [1 if A[i]%2==1 else 0 for i in range(N)]

if sum(k)%2 == 0:
    print("YES")
else:
    print("NO")

# Fixed code:
import numpy as np

N = int(input())
a = input().split(" ")
A = [int(i) for i in a]

k = [1 if A[i]%2==1 else 0 for i in range(N)]

if sum(k)%2 == 0:
    print("YES")
else:
    print("NO")
# Buggy code:
n=int(input())
a=list(map(int,input().split()))
s=0
for i in range(n):
  s+=1 if a[i]%2==1
print('YES' if s%2==0 else 'NO')

# Fixed code:
n=int(input())
a=list(map(int,input().split()))
ood_cnt=0
even_cnt=0
for i in range(n):
    if a[i]%2==0:
        even_cnt+=1
    else:
        ood_cnt+=1
if ood_cnt%2==0:
    print("YES")
else:
    print("NO")
# Buggy code:
N=int(input())
L=list(map(int,input().split()))
count=0
for i in range N:
	if L[i]%2==1:
		count+=1
if count%2==1:
	print("NO")
else:
	print("YES")

# Fixed code:
N = int(input())
L = list(map(int,input().split()))
c = 0
for i in range(N):
  if (L[i]%2 != 0):
    c += 1
if (c%2 == 0):
  print('YES')
else:
  print('NO')
# Buggy code:
N = input().split()
K = int(N[0])
NN = input().split()
even = [int(n) for n in NN if int(n) % 2 == 0]
odd = [int(n) for n in NN if int(n) % 2 != 0]
if K % 2 == 0:
    print("YES")
else:
    if len(even) < len(odd):
        print("NO")
    else:
        print("YES")

# Fixed code:
N = input().split()
K = int(N[0])
NN = input().split()
odd = [n for n in [int(N) for N in NN] if n % 2 != 0]

if len(odd) % 2 != 0:
    print("NO")
else:
    print("YES")
# Buggy code:
n=int(input())
a=[int(i) for i in input().split()]
g=[]
k=[]
for i in a:
    if i%2==0:
        g+=[i]
        g=[sum(g)]
    else:
        k+=[i]
        if len(k)%2==0:
            g+=[sum(k)]
            k=[]
    print(g,k)
print('YES'if g==[] or k==[] else 'NO')

# Fixed code:
n=int(input())
a=[int(i) for i in input().split()]
g=[]
k=[]
for i in a:
    if i%2==0:
        g+=[i]
        g=[sum(g)]
    else:
        k+=[i]
        if len(k)%2==0:
            g+=[sum(k)]
            k=[]

print('YES'if g==[] or k==[] else 'NO')
# Buggy code:
N=int(input())
L=list(map(int,input().split()))
L=[i for i in L if i%2==1]
if len(L)==1:
  print("NO")
else:
  print("YES")

# Fixed code:
N=int(input())
L=list(map(int,input().split()))
L=[i for i in L if i%2==1]
if len(L)%2==1:
  print("NO")
else:
  print("YES")
# Buggy code:
N = int(input())
A = list(map(int, input().split()))

odd = 0
even = 0

for a in A:
    if a % 2 == 0:
        even += 1
    else:
        odd += 1

if odd % 2 == 1:
    print("YES")
else:
    print("NO")


# Fixed code:
N = int(input())
A = list(map(int, input().split()))

odd = 0
for a in A:
    if a%2==1:
        odd += 1
print("YES" if odd%2==0 else "NO")
# Buggy code:
f=lambda x:int(x)%2
n=int(input())
a=list(map(f,input().split())).count(1)
print([YNEOS][n>1 and a%2>0::2]

# Fixed code:
f=lambda x:int(x)%2
n=int(input())
a=list(map(f,input().split())).count(1)
print('YNEOS'[n>1 and a%2>0::2])
# Buggy code:
#! /usr/bin/python3

N = int(input())
A = list(map(int, input().split()))

even_count = 0
odd_count = 0

for i in range(N):
    if A[i] % 2 == 0:
        even_count += 1
    else:
        odd_count += 1
    
if odd_count % 2 == 1:
    if even_count % 2 == 0:
        print("YES")
    else:
        print("NO")

else:
    even_count += odd_count // 2
    if even_count % 2 == 1:
        print("NO")
    else:
        print("YES")



# Fixed code:
#! /usr/bin/python3

N = int(input())
A = list(map(int, input().split()))

even_count = 0
odd_count = 0

for i in range(N):
    if A[i] % 2 == 0:
        even_count += 1
    else:
        odd_count += 1
    
even_count += odd_count // 2
odd_count = odd_count % 2

if odd_count == 1:
    print("NO")
else:
    print("YES")
# Buggy code:
#!/usr/bin/env python3
import sys
# input = sys.stdin.r/eadline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    N = INT()
    A=[]
    B = []
    for _ in range(N):
        a,b = MAP()
        A.append(a)
        B.append(b)

    answer = 0
    for i in range(N-1,-1,-1):
        need = B[i]-(answer+A[i])%B[i]
        answer += need
    print(answer)
    return

if __name__ == '__main__':
    main()


# Fixed code:
#!/usr/bin/env python3
import sys
# input = sys.stdin.r/eadline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    N = INT()
    A=[]
    B = []
    for _ in range(N):
        a,b = MAP()
        A.append(a)
        B.append(b)

    answer = 0
    for i in range(N-1,-1,-1):
        if (answer+A[i])%B[i] > 0:
            need = B[i]-(answer+A[i])%B[i]
            answer += need
    print(answer)
    return

if __name__ == '__main__':
    main()
# Buggy code:
n = int(input())
A, B = [], []
for _ in range(n):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
result = 0
for a, b in zip(A[::-1], B[::-1]):
    piyo = (a+result) % b
    if piyo == 0:
        continue
    result += b - piyo
print(result

# Fixed code:
n = int(input())
A = []
B = []
for _ in range(n):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

ans = 0
for a, b in zip(A[::-1], B[::-1]):
    a += ans
    if a % b == 0:
        continue
    ans += b - a % b

print(ans)
# Buggy code:

N = int(input())
total = 0
A = [None] * N
B = [None] * N
for i in range(N):
    A[i], B[i] = map(int, input().split())
	
for t in range(N):
	i = N + 1 - t
	a = A[i] % B[i]
	if a == 0:
		pass
	else:
		total = B[i]-a:
		for j in range (i):
			A[i] = A[i] + B[i] - a


print (total)
	


# Fixed code:

N = int(input())
total = 0
A = [None] * N
B = [None] * N
for i in range(N):
    A[i], B[i] = map(int, input().split())
	
for i in range(N-1,-1,-1):
	b = A[i] + total
	a = b % B[i]
	if a == 0:
		pass
	else:
		total = total + B[i] - a


print (total)
# Buggy code:
n = int(input())
a, b = [], []
for i in range(n):
	a[i], b[i] = map(int, input().split())

ans = 0
for i in range(n)[::-1]:
	a_, b_ = a[i] + ans, b[i]
	if a_%b_ != 0:
		ans += ((a_ // b_ + 1) * b_ - a_)

print(ans)


# Fixed code:
n = int(input())
a, b = [], []
for i in range(n):
    a_, b_ = 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)
# Buggy code:
n = int(input())
a, b = [0]*n,[0]*n
count = 0
for i in range(n):
    a[i],b[i] = map(int,input().split())

while(n > 0):
    count += (a[n-1]+count)%b[n-1]
    n-=1

print(count)

# Fixed code:
n = int(input())
a, b = [0]*n,[0]*n
count = 0
for i in range(n):
    a[i],b[i] = map(int,input().split())

while(n > 0):
    n -= 1
    count += (b[n] if (a[n]+count)%b[n]!=0 else 0)-(a[n]+count)%b[n]

print(count)
# Buggy code:
n = int(input())
a = []
b = []

for _ in range(n):
    c, d = map(int, input().split())
    a.append(c)
    b.append(d)

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

# Fixed code:
n = int(input())
a = []
b = []

for _ in range(n):
    c, d = map(int, input().split())
    a.append(c)
    b.append(d)

s = 0
for i in range(n-1, -1, -1):
    r = (a[i] + s) % b[i]
    if r != 0:
        s += b[i] - r
    
print(s)
# Buggy code:
N = int(input())
A = []
B = []
for i in range(N):
    a, b = 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)

# Fixed code:

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):
    if (A[N-i-1]+ans)%B[N-i-1]!=0:
        ans += B[N-i-1]-(A[N-i-1]+ans)%B[N-i-1]
print(ans)
# Buggy code:
n = int(input())
a=[]
for i in range(n):
  x,y=map(int,input().split())
  a.append([x,y])
ans=0
a=a[::-1]
for i,j in a:
  ans+=j- (i+ans)%j if (i+ans)%j else j
print(ans)

# Fixed code:
n = int(input())
a=[]
for i in range(n):
  x,y=map(int,input().split())
  a.append([x,y])
ans=0
a=a[::-1]
for i,j in a:
  ans+=j- (i+ans)%j if (i+ans)%j else 0
print(ans)
# Buggy code:

if __name__ == "__main__":
    N = int(input())
    A = N*[0]
    B = N*[0]
    for i in range(N):
        A[i],B[i] = list(map(int,input().split()))

    buttons = 0

    for i in range(N-1,-1,-1):
        buttons += B[i] - (A[i]+buttons) % B[i]

    print(buttons)



# Fixed code:

if __name__ == "__main__":
    N = int(input())
    A = N*[0]
    B = N*[0]
    for i in range(N):
        A[i],B[i] = list(map(int,input().split()))

    buttons = 0

    for i in range(N-1,-1,-1):
            a = B[i] - (A[i]+buttons) % B[i]
            if a != B[i]:
                buttons += a

    print(buttons)
# Buggy code:
import numpy as np

N = int(input())
A = np.zeros(N).astype(int)
B = np.zeros(N).astype(int)

for i in range(N):
    A[i], B[i] = map(int, input().split())

ans = 0
for i in reversed(range(N)):
  	A[i] = A[i] + ans
    if A[i] % B[i] == 0:
        continue
    elif B[i] - A[i] > 0:
        ans += B[i] - A[i]
    else:
        ans += B[i] - (A[i] % B[i])
    
print(ans)

# Fixed code:
import numpy as np

N = int(input())
A = np.zeros(N).astype(int)
B = np.zeros(N).astype(int)

for i in range(N):
    A[i], B[i] = map(int, input().split())

ans = 0
for i in reversed(range(N)):
    A[i] = A[i] + ans
    if A[i] % B[i] == 0:
        continue
    else:
        ans += B[i] - (A[i] % B[i])
    
print(ans)
# Buggy code:
N = int(input())
Array = []
for i in range(N):
  Array.append(tuple(map(int,input().split())))

res = 0
for i in reversed(range(N)):
  a,b = Array[i]
  a+=res
  res+ = b-a%b
print(res)

# Fixed code:
N = int(input())
Array = []
for i in range(N):
  Array.append(tuple(map(int,input().split())))

res = 0
for i in reversed(range(N)):
  a,b = Array[i]
  a+=res
  res+= ((b-a%b)%b)
print(res)
# Buggy code:
N = int(input())
AB = [tuple(map(int,input().split())) for i in range(N)]
z = 0
for a,b in AB[::-1]:
    z += -(z+a)%b
    print(a,b,z)
print(z)


# Fixed code:
N = int(input())
AB = [tuple(map(int,input().split())) for i in range(N)]
z = 0
for a,b in AB[::-1]:
    z += -(z+a)%b
print(z)
# Buggy code:
n = int(input())
l = []
r = []
for _ in range(n):
  a, b = map(int, input().split())
  l.append(a)
  r.append(b)
before_sa = 0
for i in range(1, n+1):
  j = 0
  a, b = l[-i], r[-i]
  a += before_sa
  if a % b == 0:
    continue
  else:
    sa = (a//b + 1) * b - a
    before_sa = sa
print(before_sa)


# Fixed code:
n = int(input())
l = []
r = []
for _ in range(n):
  a, b = map(int, input().split())
  l.append(a)
  r.append(b)
before_sa = 0
for i in range(1, n+1):
  a, b = l[-i], r[-i]
  a += before_sa
  if a % b == 0:
    continue
  else:
    sa = (a//b + 1) * b - a
    before_sa += sa
print(before_sa)
# Buggy code:
N = int(input())
A = []
B = []
total_add = 0
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)


ans = 0
inc = 0
for i in reversed(range(N)):
    a = A.pop(-1)
    a += total_add
    if a % b == 0:
        inc = 0
    else:
        inv = b - a % b
    b = B.pop(-1)
    total_add += inc

print(total_add)


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


inc = 0
for i in reversed(range(N)):
    a = A.pop(-1)
    a += total_add
    b = B.pop(-1)
    inc = b - a % b if a % b != 0 else 0
    total_add += inc

print(total_add)
# Buggy code:
def main():
    N, *AB = map(int, open(0).read().split())
    ans = 0
    for i in range(N - 1, 0, -1):
        a = AB[i * 2 + 0]
        b = AB[i * 2 + 1]
        ans += (b - a - ans) % b
    print(ans)

if __name__ == "__main__":
    main()


# Fixed code:
def main():
    N, *AB = map(int, open(0).read().split())
    ans = 0
    for i in range(N):
        index = N - 1 - i
        a = AB[index * 2 + 0] + ans
        b = AB[index * 2 + 1]
        remainder = a % b
        ans += (b - remainder) % b
    print(ans)

if __name__ == "__main__":
    main()
# Buggy code:
from sys import stdin
import fractions

n = int(stdin.readline().rstrip())
li = [[0,0]]+[list(map(int,stdin.readline().rstrip().split())) for _ in range(n)]
li = li[::-1]
count = 0
for i in range(n):
    if li[i][0]%li[i][1] != 0:
        count += li[i][1]-li[i][0]%li[i][1]
        li[i+1][0] += count
    print(li)
print(count)

# Fixed code:
from sys import stdin
import fractions

n = int(stdin.readline().rstrip())
li = [[0,0]]+[list(map(int,stdin.readline().rstrip().split())) for _ in range(n)]
li = li[::-1]
count = 0
for i in range(n):
    count += -li[i][0]%li[i][1]
    li[i+1][0] += count
print(count)
# Buggy code:
n = int(input())
a = []
b = []

for i in range(n):
  a_temp,b_temp = map(int,input().split())
  a.append(a_temp)
  b.append(b_temp)


ans = 0
for i in range(n):
  ans += (-(a[i] + ans)) % b[i]
print(ans)

# Fixed code:
n = int(input())
a = []
b = []

for i in range(n):
    a_temp, b_temp = map(int, input().split())
    a.append(a_temp)
    b.append(b_temp)

ans = 0
a = a[::-1]
b = b[::-1]
for i in range(n):
    ans += (-(a[i] + ans)) % b[i]
print(ans)
# Buggy code:
n = int(input())
a, b = [0]*n, [0]*n
for i in range(n):
	a[i], b[i] = map(int, input().split())

ans = 0
for i in range(n)[::-1]:
	a_, b_ = a[i] + p, b[i]
	if a%b != 0:
		ans += (a_ // b_ + 1) * b_ - a_

print(p)

# Fixed code:
n = int(input())
a = [0] * n
b = [0] * n

for i in range(n):
  a[i], b[i] = map(int, input().split())

ans = 0
for i in range(n-1, -1, -1):
  a[i] += ans
  if a[i] % b[i] != 0:
    ans += ((a[i] // b[i]) + 1) * b[i] - a[i]

print(ans)
# Buggy code:
n = int(input())
ab = [list(map(int, input().split())) for _i in range(n)]

x = 0
r = 0
for i in range(n-1, -1, -1):
    a, b = ab[i]
    r += (x+a)%b
    x = r
print(r)

# Fixed code:
n = int(input())
ab = [list(map(int, input().split())) for _i in range(n)][::-1]

r = 0
for i in range(n):
    a, b = ab[i]
    if (r+a)%b==0:
        continue
    r += b-(r+a)%b
print(r)
# Buggy code:
t = int(input())
a = list()

for _ in range(t):
    
    c,d = map(int,input().split())
    a.append((c,d))
    
a.reverse()

g = 0

for i in range(t):
    
    f = (a[i][0]+g)%a[i][1]
    f = a[i][1]-f
    g += f

print (g)

# Fixed code:
t = int(input())
a = list()

for _ in range(t):
    
    c,d = map(int,input().split())
    a.append((c,d))
    
a.reverse()

g = 0

for i in range(t):
    
    f = (a[i][0]+g)%a[i][1]
    if(f!=0):
        f = a[i][1]-f
    g += f

print (g)
# Buggy code:
n = int(input())
abl = [list(map(int,input().split())) for nesya in range(n)]
c = 0
for ab in abl:
  a = ab[0] + c
  b = ab[1]
  if a%b == 0:
    continue
  c += (b - a%b)
print(c)

# Fixed code:
n = int(input())
abl = [list(map(int,input().split())) for nesya in range(n)]
c = 0
abl = abl[::-1]
for ab in abl:
  a = ab[0] + c
  b = ab[1]
  if a%b == 0:
    continue
  c += (b - a%b)
print(c)
# Buggy code:
def count_baisuu(a, b):
  return b - (a % b)
  
def main():
  n = int(input())
  alist = []
  blist = []
  ab = []
  for i in range(n):
    a, b = input_list()
    ab.append((a, b))
    alist.append(a)
    blist.append(b)
    
  dp = [0]*n
  i = n - 1
  for a, b in reversed(ab):
    if i != n-1:
      a += sum(dp)
    if a % b == 0:
      continue
    if a < b:
      dp[i] = b - a
    else:
      dp[i] = count_baisuu(a, b)
    i -= 1
  print(sump(dp))
  
  

import math
import fractions
from functools import reduce


def input_list():
  return map(int, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
main()

# Fixed code:
def count_baisuu(a, b):
  return b - (a % b)
  
def main():
  n = int(input())
  alist = []
  blist = []
  ab = []
  for i in range(n):
    a, b = input_list()
    ab.append((a, b))
    alist.append(a)
    blist.append(b)
  
  s = 0
  res = 0
  for i in range(n-1, -1, -1):
    alist[i] += res
    
    if alist[i] % blist[i] == 0:
      continue
    res += blist[i] - (alist[i]%blist[i])
  print(res)
  exit()
  dp = [0]*n
  i = n - 1
  res = 0
  for a, b in reversed(ab):
    if i != n-1:
      res += dp[i]
    a += res
    if a % b == 0:
      continue
    if a < b:
      dp[i] = b - a
    else:
      dp[i] = count_baisuu(a, b)
    i -= 1
  print(dp)
  
  

import math
import fractions
from functools import reduce


def input_list():
  return map(int, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
main()
# Buggy code:
n=int(input())
a=[0]*n;b=a[:]
for i in range(n):
  c,d=map(int,input().split())
  a[i]=c;b[i]=d
ans=0
for i in range(n)[::-1]:
  a[i]+=ans
  ans+=a[i]%b[i]
print(ans)


# Fixed code:
n=int(input())
a=[0]*n;b=a[:]
for i in range(n):
  c,d=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)
# Buggy code:
n = int(input())
a,b=[],[]

for i in range(n):
    x,y = 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
    ok=b[i]-A%b[i]
    OK+=ok

print(OK)


# Fixed code:
n = int(input())
a,b=[],[]

for i in range(n):
    x,y = 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
    ok=-(-A//b[i])*b[i]-A
    OK+=ok

print(OK)
# Buggy code:
n = int(input())
abl = []
for _ in range(n):
   a,b = map(int,input().split())
   abl.append((a,b))

abl = reversed(abl)

ans = 0
for a,b in abl:
    a += ans
    ans += b - (a % b)

print(ans)

# Fixed code:
n = int(input())
abl = []
for _ in range(n):
   a,b = map(int,input().split())
   abl.append((a,b))

abl = reversed(abl)

ans = 0
for a,b in abl:
    a += ans
    if a % b != 0:
        ans += b - (a % b)

print(ans)
# Buggy code:
n=int(input())
a=[list(map(int,input().split())) for _ in range(n)]
p=0
for i in range(n):
  b=(a[i][0]+p)%a[i][1]
  if b!=0:
    p+=a[i][1]-b
print(p)

# Fixed code:
n=int(input())
a=[list(map(int, input().split())) for i in range(n)]
a=a[::-1]
count=0
for i in range(n):
    b=a[i][0]+count
    if b%a[i][1]!=0:
        count+=a[i][1]-b%a[i][1]
print(count)