A, B = map(int, input().split()) n = 0 for i in range(100000): n = A * n + B if n == 0: print(i + 1) exit() if abs(n) > 10 ** 18: print(-1) exit() print(-1)