#include <bits/stdc++.h>
using namespace std;
const long long M = 1000000007;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  while (t--) {
    int a, b;
    cin >> a >> b;
    int c, d, ans = -1;
    cin >> c >> d;
    map<int, int> ump;
    for (int i = 0; i < 10000; i++) {
      ump[b + a * i] = 1;
    }
    for (int i = 0; i < 10000; i++) {
      if (ump[d + c * i] == 1) {
        ans = d + c * i;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
