#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  for (int i = 0; i < 10001; ++i) {
    if ((d - b + i * c) >= 0 && (d - b + i * c) % a == 0) {
      cout << d + i * c << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
