A,B=map(int,input().split()) N = 0 for i in range(1,24000): N = N*A+B if N==0: print(i) exit() print(-1)