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