l, r, c = map(int, input().split()) a = c%1000 min_cost = [] for i in range(1, 1001): x = 1000 - i * a if x >= 0: min_cost.append(x) else: min_cost.append(2000 - i * a) if r - l < 1000: print(min(min_cost[l-1: r])) else: print(min(min_cost))