#ifdef LOCAL111 #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #define _USE_MATH_DEFINES #include const int INF = 1e9; using namespace std; template ostream& operator<< (ostream& os, const pair& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os; } #define endl '\n' #define ALL(a) (a).begin(),(a).end() #define SZ(a) int((a).size()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #ifdef LOCAL111 #define DEBUG(x) cout<<#x<<": "<<(x)< void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;} #else #define DEBUG(x) true template void dpite(T a, T b){ return; } #endif #define F first #define S second #define SNP string::npos #define WRC(hoge) cout << "Case #" << (hoge)+1 << ": " template void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;} template bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;} template bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;} template long long gcd(T x, T y){ return y==0 ? x : gcd(y, x%y); } template long long lcm(T x, T y){ return x/gcd(x,y)*y; } //res.first*a+res.second*b == 1 となるresを返す (a,bは互いに素) pair extgcd(long long a,long long b) { if(b==1){ return pair(0,1); } pair t=extgcd(b,a%b); return pair(t.second,t.first-a/b*t.second); } //modの逆元を返す long long inverse(long long a,long long modl) { return (extgcd(modl,a).second+modl)%modl; } // x = second (mod first) 0以上の最小解 long long garner(const vector>& ex) { int n = ex.size(); long long res = 0; long long k = 1; for(int i = 0; i < n; ++i) { long long x, m; tie(m,x) = ex[i]; x = (x%m+m)%m; long long g = gcd(k,m); if((x-res)%g != 0) return -1; long long inv = inverse(k/g,m/g); long long v = ((x-res)/g%m+m)%m*inv%m; long long nk = k/g*m; DEBUG(v); DEBUG(m); DEBUG(nk); res = (res+(v%nk)*(k%nk)%nk)%nk; k = k/g*m; } return res; } long long garner(const vector& x, const vector& mod){ int n = x.size(); vector> v(n); for(int i = 0; i < n; ++i) { v[i] = {mod[i],x[i]}; } return garner(v); } long long use_garner(vector& x, vector& mod){ int n = x.size(); for(int i = 0; i < n; ++i) { x[i] %= mod[i]; } if(n == 1) return x[0]; long long g = gcd(mod[0],mod[1]); for(int i = 2; i < n; ++i) { g = gcd(g,mod[i]); } vector> ex(n); for(int i = 0; i < n; ++i) { mod[i] /= g; if(x[i]%g != 0) return -1; x[i] /= g; ex[i] = {mod[i],x[i]}; } ex.erase(unique(ex.begin(), ex.end()),ex.end()); for(int i = 0; i < n-1; ++i) { if(ex[i].first == ex[i+1].second) return -1; } dpite(ALL(ex)); return garner(ex); } typedef long long int LL; typedef unsigned long long ULL; typedef pair P; void ios_init(){ //cout.setf(ios::fixed); //cout.precision(12); #ifdef LOCAL111 return; #endif ios::sync_with_stdio(false); cin.tie(0); } int main() { ios_init(); int n = 3; vector x(n); vector y(n); while(cin >> x[0] >> y[0]){ cin >> x[1] >> y[1]; cin >> x[2] >> y[2]; auto ans = garner(x,y); if(ans == 0) ans = lcm(lcm(y[0],y[1]),y[2]); cout << ans << endl; } return 0; }