# a = int(input())
# b = 0
# if a <= 500:
# b = a * 70 / 100
# elif 500 < a and a <= 1500:
# b = 350 + (a - 500) * 40 / 100
# elif 1500 < a and a <= 4500:
# b = 750 + (a - 1500) * 15 / 100
# elif 4500 < a and a <= 10000:
# b = 1200 + (a - 4500) * 5 / 100
# else :
# b = 1475 + (a - 10000) * 2 / 100
# print('%.0f' %b)
# x = input()
# s = 0
# for i in range(1, len(x)):
# if x[i-1] == x[i]:
# s += 5
# else :
# s += 10
# print(s + 10)
a = input()
sum = 0
for i in range(len(a)):
x, y = input().split()
x = int(x)
y = int(y)
n = y % x
sum += n
print(sum)