import math N, B = map(int, input().split()) # Expected value = (N - 1) / 2 if (N - 1) % 2 == 0: print(((N - 1) // 2) % B) else: # denominator is 2, so inverse of 2 mod B must exist if math.gcd(2, B) != 1: print("NaN") else: inv2 = pow(2, -1, B) print((N - 1) * inv2 % B)