import math n,k=map(int,input().split()) if k==0 or k>n: print(0) elif n%2==0: print(n-2) else: if k==math.ceil(n/2): print(n-1) else: print(n-2)