a, b = map(int, input().split()) if a == 0: print(1) else: b = -b x = 0 ans = 0 while x % a == 0: ans += 1 x = x // a + b if x == 0: print(ans) break else: print(-1)