l, r, c = map(int, input().split()) if r - l > 10000: print(0) else: ans = float("inf") for i in range(l, r + 1): if c * i % 1000 == 0: ans = 0 else: ans = min(ans, 1000 - (c * i) % 1000) print(ans)