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