// #define DEBUG_ON #pragma GCC optimize("Ofast") #include #define timer_start() \ chrono::system_clock::time_point start, end; \ start = chrono::system_clock::now(); #define timer_wrap() \ end = chrono::system_clock::now(); \ double time = static_cast( \ chrono::duration_cast(end - start).count() / \ 1000.0); \ printf("time %lf[ms]\n", time); \ start = end; #define optimize_cin() \ cin.tie(0); \ ios::sync_with_stdio(false) using namespace std; using ll = long long; #define INF 1000000000 // 10^9 template bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { // input int a, b; cin >> a >> b; #ifdef DEBUG_ON timer_start(); #endif if ((a > 0 && b > 0) || (a < 0 && b < 0)) { cout << -1 << endl; return 0; } ll n = 0; int d = 0; // set already; do { d++; n = (a * n) + b; // if (already.count(n) > 0) { // if (true) { // cout << -1 << endl; // return 0; // } // already.insert(n); } while (n != 0); cout << d << endl; #ifdef DEBUG_ON timer_wrap(); #endif return 0; }