l,r,m=map(int,input().split()) l%=m r%=m if l>r: print(min(m,l-r+1)) else: print(min(m,r-l+1))