a,b=map(int,input().split()) n=0 if b==0: print(1) elif a==-1: print(2) else: for i in range(100): n=a*n+b if n==0: print(i+1) break else: print(-1)