import sys sys.setrecursionlimit(5*10**5) input = sys.stdin.readline from collections import defaultdict, deque, Counter from heapq import heappop, heappush from bisect import bisect_left, bisect_right from math import gcd l,r,c = map(int,input().split()) ans = float("inf") for i in range(l, min(r, l+1000)+1): if c * i % 1000 == 0: print(0) break tmp = 1000 - c * i % 1000 ans = min(ans, tmp) else: print(ans)