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