L,R,C=map(int,input().split()) D={} ans=1000 for x in range(L,R+1): d=x*C%1000 if d==0: print(0) exit() d=1000-d if d not in D: D[d]=1 ans=min(ans,d) else: break print(ans)