#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d;
  cin >> b >> a >> d >> c;
  bool flag(false);
  for (int i = 0; i < 100000; i++) {
    if (a == c) {
      cout << a << endl;
      flag = true;
      break;
    } else if (a > c)
      c += d;
    else if (a < c)
      a += b;
  }
  if (!flag) cout << "-1" << endl;
  return 0;
}
