#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &s, const vector<T> &c) {
  s << "[ ";
  for (auto it : c) s << it << " ";
  s << "]\n";
  return s;
}
int main() {
  int a, b, c, d;
  cin >> b >> a >> d >> c;
  map<int, int> meow;
  for (int i = 0; i < 7122; ++i) {
    meow[a] = 1;
    a += b;
  }
  for (int i = 0; i < 7222; ++i) {
    if (meow[c]) {
      cout << c << '\n';
      return 0;
    }
    c += d;
  }
  cout << "-1\n";
  return 0;
}
