l,r,c=map(int,input().split()) ans = 10**20 for i in range(l,min(r,l+1000)): t=i*c ans = min(1000-t%1000,ans) print(ans)