#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int a, b, c, d;
  cin >> a >> b >> c >> d;
  map<long long int, long long int> mp;
  for (long long int i = 0; i < 101; i++) mp[b + i * a]++;
  for (long long int j = 0; j < 101; j++) {
    if (mp.find(d + j * c) != mp.end()) {
      cout << d + j * c;
      return 0;
    }
  }
  cout << -1;
}
