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