L,R,M=map(int,input().split()) if R-(L-1)>=M: print(M) else: num1=L%M num2=R%M if num2>num1: print(num2-(num1-1)) else: print(M-1-(num1-1)+num2+1)