import typing def inv_gcd(a: int, b: int) -> typing.Tuple[int, int]: a %= b if a == 0: return (b, 0) s = b t = a m0 = 0 m1 = 1 while t: u = s // t s -= t * u m0 -= m1 * u s, t = t, s m0, m1 = m1, m0 if m0 < 0: m0 += b // s return (s, m0) def inv_mod(x: int, m: int) -> int: z = inv_gcd(x, m) return z[1] def crt(r: typing.List[int], m: typing.List[int]) -> typing.Tuple[int, int]: r0 = 0 m0 = 1 for r1, m1 in zip(r, m): r1 %= m1 if m0 < m1: r0, r1 = r1, r0 m0, m1 = m1, m0 if m0 % m1 == 0: if r0 % m1 != r1: return (0, 0) continue g, im = inv_gcd(m0, m1) u1 = m1 // g if (r1 - r0) % g: return (0, 0) x = (r1 - r0) // g % u1 * im % u1 r0 += x * m0 m0 *= u1 if r0 < 0:r0 += m0 return (r0, m0) n = int(input()) e = int(input()) # x^2 + y 5^e - n = 0 has integer solution ? # e = 0 -> ok. if e == 0: print(0) exit() # e > 0 -> n = 1, 4 mod 5 and n != mod 5^e if not(n % 5 == 1 or n % 5 == 4 or n % (5**e) == 0): print("NaN") exit() if n % (5**e) == 0: print(0) else: # e = 1 # x^2 - n = 0 (mod 5) a = -1 for i in range(5): if (i * i - n) % 5 == 0: a = i break assert a >= 0 # e = 2, 3, ..., for t in range(2, e+1): c = (a * a - n) // (5 ** (t-1)) y = -1 for yy in range(5): if (2 * a * yy + c) % 5 == 0: y = yy break assert y >= 0 b = (a + 5 ** (t-1) * y) % (5 ** t) a = b if a >= 2 ** 29: a -= 5 ** e print(a)