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