#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; ll gcd(ll a, ll b){ if(b == 0) return a; return gcd(b, a%b); } ll lcm(ll a, ll b){ return a*b / gcd(a, b); } int main(){ ll x1, y1, x2, y2, x3, y3; cin >> x1 >> y1; cin >> x2 >> y2; cin >> x3 >> y3; ll n1, n2; bool success = false; ll l = lcm(y1, y2); for(int i = 0; i <= y2 && !success; i++){ n1 = x1 + y1 * i; if (n1 > 0 && n1 % y2 == x2) { for(int j = 0; j <= y3 && !success; j++){ n2 = n1 + l * j; if (n2 % y3 == x3) { success = true; break; } } } } if (success) { cout << n2 << endl; } else { cout << -1 << endl; } return 0; }