def solve(): n, m = map(int, input().split()) if n == m: return 0 n %= m for ret in range(1, n): if n < ret: break n %= ret return ret - 1 print(solve())