X=input().split() L=int(X[0]) R=int(X[1]) C=int(X[2]) t=1000 a=t b=(L*C)%t while L<=R: if b==0: print(0) exit(0) a=min(a,t-b) b=(b+C)%t L+=1 print(a)