#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
const long long INF = 1e9 + 5;
vector<long long> v;
map<long long, long long> mp;
void solve() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  long long cnt = 0;
  long long tmpb = b, tmpd = d;
  for (long long i = 0; i <= 1e5 + 5; i++) {
    tmpb = b + cnt * a;
    tmpd = d + cnt * c;
    mp[tmpb]++;
    mp[tmpd]++;
    cnt++;
  }
  for (auto x : mp) {
    if (x.second >= 2) {
      cout << x.first;
      return;
    }
  }
  cout << "-1\n";
}
int32_t main() {
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
}
