#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; vector

fac(bool myon){ map mp; vector

ret; ll x=p-1; for(ll i=2; i*i<=x; i++){ if(x%i==0){ int f=0; while(x%i==0){ x/=i; f++; } mp[i]+=f; } } if(x>1) mp[x]++; if(!myon){ for(auto q:mp) ret.push_back(q); return ret; } x=p+1; for(ll i=2; i*i<=x; i++){ if(x%i==0){ int f=0; while(x%i==0){ x/=i; f++; } mp[i]+=f; } } if(x>1) mp[x]++; for(auto q:mp) ret.push_back(q); return ret; } vector order(Mat a){ ll x=(p-1)*(p+1); bool myon=0; if(matpow(a, x)!=e){ x=p*(p-1); }else{ myon=1; } vector

v=fac(myon); vector ret; for(auto q:v){ ll r=q.first; int f=q.second; ll y=1; for(int i=0; i1) ret.push_back(y); } if(!myon) ret.push_back(p); return ret; } 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[0][0]*a[1][1]%p==a[0][1]*a[1][0]%p){ e=matpow(a, p-1); }else{ e[0][0]=e[1][1]=1; } vector vd=order(a); if(vd.empty()){ cout<<-1< vf(k); for(int i=0; i