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