N,K = [int(i) for i in input().split()] if N<K or K==0 or N==K==1 or N==K==2: print(0) else: if N//2+1 == K: print(N-1) else: print(N-2)