import sys input = sys.stdin.readline from math import gcd N,mod=list(map(int,input().split())) ANS=N-1 if (N-1)%2==0: ANS//=2 else: if gcd(2,mod)!=1: print("NaN") exit() else: ANS=ANS*pow(2,-1,mod)%mod print(ANS%mod)