#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-6;
const double Pi = acos(-1);
bool cmp(const pair<int, int> &fi, const pair<int, int> &se) {
  return fi.second < se.second;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
int dr[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dc[] = {0, 1, 1, 1, 0, -1, -1, -1};
int x[1000005];
int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  for (int i = b; i <= 1000005; i += a) x[i] = 1;
  for (int i = d; i <= 1000005; i += c)
    if (x[i]) {
      cout << i << endl;
      return 0;
    }
  cout << -1 << endl;
  return 0;
}
