#include #include using namespace std; typedef long long ll; ll gcd(ll a, ll b){ return b == 0 ? a : gcd(b, a%b); } bool comp(pair a, pair b){ if(a.second != b.second) a.second > b.second; else a.first > b.first; } int main(){ pair p[3]; for(int i = 0; i < 3; i++) cin >> p[i].first >> p[i].second; sort(p, p+3, comp); bool valid = true; for(int i = 1; i < 3; i++){ for(int j = 0; j < i; j++){ if(p[i].second%p[j].second) continue; valid &= (p[i].first%p[j].second)==p[j].first; } } if(!valid){ cout << -1 << endl; return 0; } ll ans = p[2].first; while(ans%p[1].second != p[1].first) ans += p[2].second; ll g = p[1].second*p[2].second/gcd(p[1].second,p[2].second); while(ans%p[0].second != p[0].first) ans += g; cout << ans << endl; return 0; }