#include #define rep(i,n) for(int i=0;i<(int)(n);i++) using namespace std; using ll = long long ; using P = pair ; using pll = pair; constexpr int INF = 1e9; constexpr long long LINF = 1e17; constexpr int MOD = 1000000007; constexpr double PI = 3.14159265358979323846; inline long long mod(long long x,long long m){ return (x % m + m) % m; } long long gcd(long long i,long long j){ if(j == 0) return i; return gcd(j,i%j); } //a*x + b*y =gcd(a,b)を満たすx、yをもとめる pair extgcd(long long a,long long b){ if(b == 0) return {1,0}; pair res = extgcd(b,a%b); long long x = res.first; long long y = res.second; return {y,x - (a/b) * y}; } pair CRT(long long b1,long long m1,long long b2,long long m2){ pair pq = extgcd(m1,m2); long long p = pq.first; long long q = pq.second; long long d = gcd(m1,m2); if((b2 - b1) % d != 0) return make_pair(0,-1); long long m = m1 * (m2 / d); //lcm(m1,m2) long long tmp = ((b2 - b1) / d * p) % (m2/d); long long r = mod(b1 + m1 * tmp, m); return make_pair(r,m); } int main(){ ll X,Y; cin >> X >> Y; rep(i,2){ ll x,y; cin >> x >> y; pll ans = CRT(X,Y,x,y); if(ans.second == -1){ cout << -1 << endl; return 0; } X = ans.first; Y = ans.second; //cout << X << " " << Y << endl; } cout << X << endl; return 0; }