A,B = map(int,input().split()) N = 0 N_list = [] i = 0 flg = False if A > 0 and B > 0: print(-1) elif abs(A) == abs(B) : print(-1) else: while flg == False: N = A*N +B if N in N_list: break N_list.append(N) i += 1 if N == 0: print(i) flg = True break if flg == False: print(-1)