#include <bits/stdc++.h>
using namespace std;
int main() {
  int b, a, d, c;
  cin >> a >> b >> c >> d;
  int k = 0;
  while (b != d) {
    if (d > b) {
      b += a;
    } else {
      d += c;
    }
    k++;
    if (k == 100005) {
      cout << "-1";
      return 0;
    }
  }
  cout << b;
  return 0;
}
