l, r, c = map(int, input().split()) m = [False]*1000 for i in range(l, r+1): mod = 1000-c*i % 1000 if m[mod]: break m[mod] = True for i in range(1000): if m[i]: print(i) break