l, r, c = map(int, input().split()) change = [] for i in range(l, r+1): a = (i * c) % 1000 if a == 0: change.append(a) else: b = 1000 - a change.append(b) print(min(change))