l,r,c=map(int,input().split()) r2=min(l+1003,r+1) ans=999 for i in range(l,r2): t=(c*i)%1000 if t==0: ans=0 else: ans=min(ans,1000-t) print(ans)