#include <bits/stdc++.h> #define rep(i,a,b) for(int i=int(a);i<=int(b);++i) using namespace std; template<class A>void pr(A a){cout << (a) << endl;} template<class A,class B>void pr(A a,B b){cout << a << " " ;pr(b);} template<class A,class B,class C>void pr(A a,B b,C c){cout << a << " " ;pr(b,c);} template<class A,class B,class C,class D>void pr(A a,B b,C c,D d){cout << a << " " ;pr(b,c,d);} typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; int INF=numeric_limits<int>::max(); // for(int j=0;j<N;++j) { int main(void) { ll MOD=pow(10,9)+7; ll a,b;cin>>a>>b; ll n = 0; int cnt=0; rep(i,-10000,10000) { cnt++; n=(n*a+b)%MOD; if(n==0) { pr(cnt); return 0; } } if(n!=0) { pr(-1); } return 0; }