N, K = map(int, input().split()) if K & 1: if N % (K+2) == 1: print(0) else: if K == N-1: print(K) exit() a = (N - 1) % (K + 2) if a & 1: print(a) else: print(a // 2) print(a) else: if N % (K+1) == 1: print(0) else: a = (N - 1) % (K + 1) if a & 1: print(a) else: print(a // 2) print(a)