#include using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- ll mo; const int MAT=2; struct Mat { ll v[MAT][MAT]; Mat(){ZERO(v);};}; Mat mulmat(Mat& a,Mat& b,int n=MAT) { ll mo2=4*mo*mo; int x,y,z; Mat r; FOR(x,n) FOR(y,n) r.v[x][y]=0; FOR(x,n) FOR(z,n) FOR(y,n) { r.v[x][y] += a.v[x][z]*b.v[z][y]; if(r.v[x][y]>mo2) r.v[x][y] -= mo2; } FOR(x,n) FOR(y,n) r.v[x][y]%=mo; return r; } Mat powmat(ll p,Mat a,int n=MAT) { int i,x,y; Mat r; FOR(x,n) FOR(y,n) r.v[x][y]=0; FOR(i,n) r.v[i][i]=1; while(p) { if(p%2) r=mulmat(r,a,n); a=mulmat(a,a,n); p>>=1; } return r; } ll modpow(ll a, ll n = mo-2) { ll r=1; while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1; return r; } Mat revmat(Mat a,int n=MAT) { Mat m; int x,y,z; FOR(x,n) FOR(y,n) m.v[x][y]=x==y; FOR(x,n) { for(y=x;y conv(Mat M) { vector V; V.push_back(M.v[0][0]); V.push_back(M.v[0][1]); V.push_back(M.v[1][0]); V.push_back(M.v[1][1]); return V; } ll modlog(int g,int a) { // return g^x=a mod a map M; ll cur=1; int sq=sqrt(mo); int i; FOR(i,sq) { M[cur]=i; cur=cur*g%mo; } ll step=cur; cur=1; ll num=0; int lp=0; while(1) { ll x=a*modpow(cur)%mo; if(lp>sq+5) return 1LL<<50; if(M.count(x)) return num+M[x]; lp++; cur=cur*step%mo; num+=sq; } } void solve() { int i,j,k,l,r,x,y; string s; cin>>mo; cin>>A.v[0][0]>>A.v[0][1]; cin>>A.v[1][0]>>A.v[1][1]; cin>>B.v[0][0]>>B.v[0][1]; cin>>B.v[1][0]>>B.v[1][1]; ll DA=((A.v[0][0]*A.v[1][1]-A.v[0][1]*A.v[1][0])%mo+mo)%mo; ll DB=((B.v[0][0]*B.v[1][1]-B.v[0][1]*B.v[1][0])%mo+mo)%mo; if((DA==0) != (DB==0)) return _P("-1\n"); if(DA==0) { //周期はP-1以内なので普通にBSGS Mat EE; EE.v[0][0]=EE.v[1][1]=1; Mat RA=revmat(A); Mat E=B,QA; QA.v[0][0]=QA.v[1][1]=1; map,int> M; M[conv(E)]=0; for(i=1;1LL*i*i<=mo;i++) { E=mulmat(RA,E); M[conv(E)]=i; QA=mulmat(QA,A); } j=i-1; Mat A2; A2.v[0][0]=A2.v[1][1]=1; ll P=0; ll ret=1LL<<60; while(P<=mo) { if(M.count(conv(A2))) ret=min(ret,(P+M[conv(A2)])); A2=mulmat(A2,QA); P+=j; } if(ret==1LL<<60) ret=-1; cout<1LL<<40) return _P("-1\n"); //detAの位数を求める ll P=mo; for(i=1;1LL*i*i<=mo;i++) if((mo-1)%i==0) { if(modpow(DA,i)==1) P=min(P,(ll)i); if(modpow(DA,(mo-1)/i)==1) P=min(P,(mo-1)/i); } // Aの位数を求める vector V[3]; FOR(i,3) { ll T=mo+1-i; for(j=1;j*j<=T;j++) if(T%j==0) V[i].push_back(j),V[i].push_back(T/j); } ll TP=mo*mo; FORR(x,V[0]) FORR(y,V[1]) if(conv(powmat(x*y+1,A))==conv(A)) TP=min(TP,x*y); FORR(x,V[0]) FORR(y,V[2]) if(conv(powmat(x*y+1,A))==conv(A)) TP=min(TP,x*y); FORR(x,V[1]) FORR(y,V[2]) if(conv(powmat(x*y+1,A))==conv(A)) TP=min(TP,x*y); //cerr<,ll> M; M[conv(E)]=0; for(i=1;1LL*i*i<=mo;i++) { E=mulmat(revA,E); M[conv(E)]=i*P; QPA=mulmat(QPA,PA); } j=i-1; Mat A2=powmat(R,A); ll count=0; ll ret=1LL<<60; while(count<=2*mo) { if(M.count(conv(A2))) { ret=min(ret,(count*P+M[conv(A2)]+R)%TP); } A2=mulmat(A2,QPA); count+=j; } if(ret==1LL<<60) ret=-1; cout<