#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair P; ll p; using arr=array; using Mat=array; Mat matmul(Mat a, Mat b){ Mat c={}; for(int i=0; i<2; i++){ for(int j=0; j<2; j++){ for(int k=0; k<2; k++){ (c[i][j]+=a[i][k]*b[k][j])%=p; } } } return c; } Mat matpow(Mat a, ll k){ Mat ap=a, ans={}; ans[0][0]=ans[1][1]=1; while(k){ if(k&1) ans=matmul(ap, ans); ap=matmul(ap, ap); k>>=1; } return ans; } Mat e; ll discretelog(Mat a, Mat b, ll d){ ll sq=1; while(sq*sq> vp(sq); vp[0]=make_pair(e, 0); for(int i=1; i x, pair y){ for(int i=0; i<2; i++){ for(int j=0; j<2; j++){ if(x.first[i][j]!=y.first[i][j]) return x.first[i][j]0){ ll t=a/b; swap(a-=t*b, b); swap(x-=t*y, y); } return (x%m+m)%m; } int main() { cin>>p; Mat a, b; cin>>a[0][0]>>a[0][1]>>a[1][0]>>a[1][1]>>b[0][0]>>b[0][1]>>b[1][0]>>b[1][1]; if(a==b){ cout<<1< vd; if(matpow(a, (p-1)*(p+1))!=e){ vd.push_back(p); vd.push_back(p-1); }else{ vd.push_back((p-1)/2); vd.push_back((p+1)/2); ll p2=4; while(vd[0]%2==0){ p2<<=1; vd[0]>>=1; } while(vd[1]%2==0){ p2<<=1; vd[1]>>=1; } vd.push_back(p2); } ll d=1; for(auto x:vd) d*=x; //cout< vf(k); for(int i=0; i