#include #include #include using lint = long long; constexpr lint INF = 1LL << 30; void solve() { lint a, b; std::cin >> a >> b; lint n = 0; std::set vis; for (int k = 0;; ++k) { n = a * n + b; if (vis.count(n) || std::abs(n) >= INF) break; if (n == 0) { std::cout << k + 1 << std::endl; return; } vis.insert(n); } std::cout << -1 << std::endl; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }