L,R,M=[int(i) for i in input().split()] if abs(R-L+1)>=M: print(M) else: L%=M R%=M if L<=R: print(R-L+1) else: print((M-L) + R+1)