import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines sys.setrecursionlimit(10 ** 7) a, b = map(int, read().split()) n = 0 for i in range(10000): n = a * n + b if n == 0: print(i + 1) exit() print(-1)