// // Created by yamunaku on 2019/12/13. // #include using namespace std; #define rep(i, n) for(int i = 0; i < (n); i++) #define repl(i, l, r) for(int i = (l); i < (r); i++) #define per(i, n) for(int i = ((n)-1); i >= 0; i--) #define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--) #define all(x) (x).begin(),(x).end() #define MOD9 998244353 #define MOD1 1000000007 #define IINF 1000000000 #define LINF 1000000000000000000 #define SP <<" "<< #define CYES cout<<"Yes"<; using mti = vector>; using vl = vector; using mtl = vector>; using pi = pair; using pl = pair; template using heap = priority_queue, function>; ll p; mtl a, b; ll det(mtl &v, ll m){ return ((v[0][0] * v[1][1] % m - v[0][1] * v[1][0] % m) % m + m) % m; } mtl prod(mtl a, mtl b, ll m){ return { {(a[0][0] * b[0][0] % m + a[0][1] * b[1][0] % m) % m, (a[0][0] * b[0][1] % m + a[0][1] * b[1][1] % m) % m}, {(a[1][0] * b[0][0] % m + a[1][1] * b[1][0] % m) % m, (a[1][0] * b[0][1] % m + a[1][1] * b[1][1] % m) % m} }; } mtl mtpow(mtl v, ll x, ll m){ mtl ans = { {1, 0}, {0, 1} }; while(x){ if(x & 1) ans = prod(ans, v, m); x >>= 1; v = prod(v, v, m); } return ans; } ll modpow(ll x, ll a, ll m){ ll ans = 1; while(a){ if(a & 1) ans = ans * x % m; a >>= 1; x = x * x % m; } return ans; } ll inv(ll x, ll m){ return modpow(x, m - 2, m); } ll nlog(ll a, ll b, ll p){ // a^(i*r+j)=b // a^j = b/a^(i*r) ll r = sqrt(p) + 10; ll ar = inv(modpow(a, r, p), p); map mp; ll tmp = b; rep(i, r){ auto itr = mp.find(tmp); if(itr == mp.end()) mp[tmp] = i; tmp = tmp * ar % p; } ll ans = LINF; tmp = 1; rep(i, r){ auto itr = mp.find(tmp); if(itr != mp.end()) ans = min(ans, (itr->second * r + i) % (p - 1)); tmp = tmp * a % p; } return ans; } mtl mtinv(mtl a, ll p){ ll d = inv(det(a, p), p); return { {a[1][1] * d % p, (-a[0][1] * d % p + p) % p}, {(-a[1][0] * d % p + p) % p, a[0][0] * d % p}, }; } ll mtlog(mtl a, mtl b, ll p){ // a^(i*r+j)=b // a^j = b/a^(i*r) ll r = sqrt(1e14); mtl ar = mtinv(mtpow(a, r, p), p); map mp; mtl tmp = b; rep(i, r){ mp[tmp] = i; tmp = prod(tmp, ar, p); } ll ans = LINF; tmp = { {1, 0}, {0, 1} }; rep(i, r){ auto itr = mp.find(tmp); if(itr != mp.end()) ans = min(ans, itr->second * r + i); tmp = prod(tmp, a, p); } return ans; } int main(){ cin >> p; a = b = mtl(2, vl(2)); rep(i, 2) rep(j, 2) cin >> a[i][j]; rep(i, 2) rep(j, 2) cin >> b[i][j]; if(a == b){ cout << 1 << endl; return 0; } ll x = det(a, p), y = det(b, p); if(x == 0){ if(y > 0){ cout << -1 << endl; return 0; } ll ans = mtlog(a, b, p); if(ans == LINF) cout << -1 << endl; else cout << ans << endl; return 0; } if(y == 0){ cout << -1 << endl; return 0; } if(x == 1){ if(y > 1){ cout << -1 << endl; return 0; } ll ans = mtlog(a, b, p); if(ans == LINF) cout << -1 << endl; else cout << ans << endl; return 0; } ll l = nlog(x, y, p); mtl tmp = mtpow(a, l, p); if(tmp == b){ cout << l << endl; return 0; } a = mtpow(a, p - 1, p); b = prod(b, mtinv(tmp, p), p); if(a == b){ cout << p - 1 + l << endl; return 0; } ll ans = mtlog(a, b, p); if(ans == LINF) cout << -1 << endl; else cout << ans * (p - 1) + l << endl; return 0; }