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