L,R,C=map(int,input().split()) ans=1000 for i in range(L,min(R+1,L+114514)): ans=min(ans,(1000-(C*i)%1000)%1000) print(ans)