#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  t = 1;
  while (t--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    while (b != d && b < 1e5) {
      b < d ? b += a : d += c;
    }
    if (b == d) {
      cout << b;
    } else {
      cout << "-1";
    }
    cout << endl;
  }
  return 0;
}
