from sys import stdin def main(): input = lambda: stdin.readline()[:-1] A, B = map(int, input().split()) if (A > 0 and B > 0) or (not A and B != 0): print(-1) return elif not A and not B: print(1) return if B % A: print(-1) else: ans, n = 0, 0 while 1: n = A * n + B ans += 1 if not n: break print(ans) main()