import sys sys.setrecursionlimit(10 ** 8) input = sys.stdin.readline def main(): A, B = [int(x) for x in input().split()] N = 0 ans = 0 while True: ans += 1 N = A * N + B if N == 0: print(ans) return if ans == 10 ** 4: break print(-1) if __name__ == '__main__': main()