#include <bits/stdc++.h>
using namespace std;
map<long long int, bool> mp1, mp2;
int main() {
  long long int n, i, b, a, c, d, sum1 = 0, sum2 = 0;
  cin >> a >> b;
  cin >> c >> d;
  sum1 = b, sum2 = d;
  mp1[b] = true;
  mp2[d] = true;
  for (;;) {
    if (sum1 > 10e5) {
      break;
    }
    sum1 += a;
    mp1[sum1] = true;
  }
  for (;;) {
    if (mp1[sum2] == true && mp2[sum2] == true) {
      cout << sum2 << endl;
      return 0;
    }
    if (sum2 > 10e5) {
      break;
    }
    sum2 += c;
    mp2[sum2] = true;
  }
  cout << "-1" << endl;
  return 0;
}
