a, b = map(int,input().split()) n = 0 for i in range(10 ** 5): n = a * n + b if n == 0: print(i + 1) exit() print(-1)