#include <bits/stdc++.h>   using namespace std;   long long a, b;   long long c, d;   int main() {     while (cin >> a >> b) {       cin >> c >> d;       for (int i = 0; i <= 1000; ++i) {         for (int j = 0; j <= 1000; ++j) {           if ((b + a * i) == (d + c * j)) {             cout << b + a * i << endl;             return 0;           }         }       }       cout <<  -1  << endl;     }   }   