#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(vector rs, 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){ T g = __gcd(res_m, ms[i]); if(res_r%g != rs[i]%g != 0){ return pair(-1, -1); } auto[x, y] = extgcd(res_m, ms[i]); T l = lcm(res_m, ms[i]); res_r = rs[i]*res_m/g%l*x%l + res_r*ms[i]/g%l*y%l; res_r = (res_r%l + l)%l; res_m = l; } 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; }