#include <bits/stdc++.h>
using namespace std;
long long ar[500005];
long long br[500005];
int main() {
  long long a, b, c, d, i, j, k, m, n, t, sum, f;
  cin >> a >> b;
  cin >> c >> d;
  for (i = 0; i < 101; i++) ar[i] = b + i * a;
  for (i = 0; i < 101; i++) br[i] = d + i * c;
  for (i = 0; i < 101; i++)
    for (j = 0; j < 101; j++)
      if (ar[i] == br[j]) {
        cout << ar[i] << endl;
        return 0;
      }
  cout << -1 << endl;
}
