l, r, c = map(int,input().split()) k = 999 for i in range(l, r+1): if i*c%1000 == 0: k = 0 break k = min(k, 1000-(i*c%1000)) print(k)