from math import gcd n,b=map(int,input().split()) g=gcd(n,b) i=pow(n,b-2,b) if(n*i-1)%b==0 and i>=0: print(i) else: print('NaN')