#include using namespace std; long long extgcd(long long a, long long b, long long &x,long long &y){ if(b == 0){x = 1,y = 0; return a;} long long g = extgcd(b,a%b,y,x); y -= a/b*x; return g; } pair CRT2(vector &A,vector &M){ long long ret = 0,lcm = 1; for(int i=0; i A(3),M(3); for(int i=0; i<3; i++) cin >> A.at(i) >> M.at(i); auto[ans,lcm] = CRT2(A,M); if(lcm == -1) cout << -1 << endl; else if(ans == 0) cout << lcm << endl; else cout << ans << endl; }