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