#include <bits/stdc++.h>
using namespace std;
template <typename P, unsigned long long int mod>
class Mod {
 public:
  Mod() : v_() {}

 public:
  template <typename V>
  P& operator=(const V&& rv) {
    static_assert(std::is_integral<V>::value, "should be integral type");
    v_ = rv;
    return v_;
  }
  template <typename V>
  P& operator+=(const V&& rv) {
    static_assert(std::is_integral<V>::value, "should be integral type");
    v_ += rv;
    return v_;
  }

 private:
  P v_;
};
const int sz = 1e5;
int main() {
  ios::sync_with_stdio(false);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  set<int> num;
  for (int i = 0; i < sz; ++i) {
    num.insert(b + i * a);
  }
  for (int i = 0; i < sz; ++i) {
    if (num.find(d + i * c) != num.end()) {
      cout << d + i * c << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
