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