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