A, B = map(int, input().split()) N = B L = [B] K = 0 if A > 0 and B > 0: K = -1 elif B == 0: K = 1 else: while N != 0: N = A * N + B if N == 0: K = len(L) + 1 break else: if N in L: K = -1 break else: L.append(N) print(K)