from math import gcd N, B = map(int, input().split()) if gcd(N, B) != 1: print('NaN') exit() x = pow(N, B-2, B) print(x)