L, R, C = map(int, input().split()) S = set() for x in range(L, R+1): t = C*x%1000 if t!=0: t=1000-t if t in S: break S.add(t) print(min(S))