#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans1, ans2, i, j, a, b, c, d;
  cin >> a >> b >> c >> d;
  for (i = 0; i < 1000; i++) {
    for (j = 0; j < 1000; j++) {
      ans1 = b + i * a;
      ans2 = d + j * c;
      if (ans1 == ans2) {
        cout << ans1, 0;
        return 0;
      }
    }
  }
  cout << -1 << endl;
}
