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