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