#include using namespace std; template T lcm(T a, T b){ return a / __gcd(a, b) * b; } template pair extgcd(T a, T b){ // ax + by = gcd(a, b) if(a == 0){ return pair(0, 1); }else if(b == 0){ return pair(1, 0); } T q = a/b; T r = a%b; auto [s, t] = extgcd(b, r); T y = s - q*t; return pair(t, y); } template pair ctr(const vector& rs, const vector& ms){ assert(rs.size() == ms.size()); int n = rs.size(); T res_r = 0; T res_m = 1; for(int i = 0;i < n; ++i){ auto[x, y] = extgcd(res_m, ms[i]); T g = __gcd(res_m, ms[i]); if((rs[i] - res_r)%g != 0)return pair(-1, -1); T tmp = (rs[i] - res_r) / g * x%(ms[i]/g); res_r += res_m * tmp; res_m *= ms[i] / g; } return pair(res_r, res_m); } void solve(){ vector r(3), m(3); for(int i = 0;i < 3; ++i){ cin >> r[i] >> m[i]; } auto[ar, am] = ctr(r, m); cout << ar << endl; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); solve(); return 0; }