N,B=map(int,input().split()) if N&1: ans=(N-1)//2%B elif B&1: ans=(N-1)*pow(2,B-2,B)%B else: ans="NaN" print(ans)