import sys f=lambda a,b:[-1,1][a%4==1or b%4==1]*f(b%a,a)if a%2else[-1,1][b%8in(1,7)]*f(a//2,b)if a else(b==1)*1 for z in sys.argv[1:]:p,q=map(int,z.split());print(f(p%q,q))