import java.io.*; import java.util.*; class Main { public static void main(String[] args) { new Main().run(); } long MOD; long[][] pow(long[][] a, long n){ long[][] ret=new long[2][2]; ret[0][0]=ret[1][1]=1; for(;n>0;n>>=1,a=mul(a,a)){ if(n%2==1) ret=mul(ret,a); } return ret; } long[][] mul(long[][] a, long[][] b){ long[][] ret=new long[a.length][b[0].length]; for(int i=0;i0;n>>=1,a=a*a%MOD){ if(n%2==1)ret=ret*a%MOD; } return ret; } // return x s.t. a^x = b && x>0 long discretelog(long a, long b){ if(a==1){ if(b==1) return 1; else return -1; }else if(a==0){ if(b==0)return 1; else return -1; } // a^(um+v) = b // a^v = b a^(-m)^u int m=(int)Math.sqrt(MOD); long pw=1; HashMap map=new HashMap<>(); for(int v=0;v<=m;++v){ map.put(pw,v); pw=pw*a%MOD; } long ima=pow(inv(a),m); long ipw=1; for(int i=0;i<=m;++i){ if(map.containsKey(b*ipw)){ long ret=i*m+map.get(b*ipw); if(ret!=0)return ret; } ipw=ipw*ima%MOD; } return -1; } long gcd(long a,long b){ if(a>b)return gcd(b,a); if(a==0)return b; return gcd(a,b%a); } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }