#include using ll = long long; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll a,b,n{0}; cin >> a >> b; set s; int cnt{0}; while (1) { n=a*n+b; ++cnt; if (!n) break; if (s.find(n)!=s.end()) { cout << "-1\n"; return 0; } s.insert(n); } cout << cnt << "\n"; return 0; }