#55373: python


ka2453@cnc.km.edu.tw (金湖國中Owen)


import sys
n=0
for i in sys.stdin:
 a,b=map(int,i.split())
 if a>0 and b>0:
  n+=a%b
print(n)