#判断有无解
def is_quadratic_residue(x, p):
if pow(x, (p-1)//2, p) == 1:
return True
else:
return False
#模幂运算
def pow_mod(a, b, m):
res = 1
while b > 0:
if b & 1:
res = (res * a) % m
a = (a * a) % m
b >>= 1
return res
#求解
def solvex(b,p):
if p%4==3:
return pow_mod(b,(p+3)//8,p)
if p%8==5 and pow_mod(b,(p-1)//4,p)==1:
return pow_mod(b,(p+3)//8,p)
if p%8==5 and pow_mod(b,(p-1)//4,p)==-1:
return pow_mod(b,(p+3)//8,p)*pow(2,(p-1)//4)
b = int(input())
p = int(input())
if(is_quadratic_residue(b,p)):
print("有解")
x = solvex(b,p)
print("解为:±",x)
else:
print("无解")