#include <bits/stdc++.h>
using namespace std;
void solve() {
  int ans = -1;
  map<int, int> m;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  m[b]++;
  m[b + a]++;
  for (int i = 2; i <= 100; ++i) {
    m[(i * a) + b]++;
  }
  if (m[d] == 1)
    ans = d;
  else if (m[d + c] == 1)
    ans = d + c;
  else {
    for (int i = 2; i <= 100; ++i) {
      int x = d + (i * c);
      if (m[x] == 1) {
        ans = x;
        break;
      }
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  for (int i = 1; i <= t; ++i) {
    solve();
  }
  return 0;
}
