#include <bits/stdc++.h>
using namespace std;
using lli = long long;
const lli OO = 1e18;
string ys = "YES", no = "NO";
void fast() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
lli di[8] = {-1, 1, 0, 0, -1, 1, -1, 1};
lli dj[8] = {0, 0, -1, 1, 1, 1, -1, -1};
vector<vector<lli>> adj;
lli frq[1000007 + 7];
bool isPrime[1000007];
vector<int> primes;
void sieve() {
  memset(isPrime, true, sizeof isPrime);
  isPrime[0] = isPrime[1] = false;
  for (int i = 4; i < 1000007; i += 2) isPrime[i] = false;
  for (lli i = 3; i * i < 1000007; i++)
    if (isPrime[i])
      for (lli j = i * i; j < 1000007; j += i + i) isPrime[j] = false;
  primes.push_back(2);
  for (int i = 3; i < 1000007; i += 2)
    if (isPrime[i]) primes.push_back(i);
}
lli gcd(lli a, lli b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
lli lcm(lli n, lli m) { return n * m / gcd(n, m); }
int main() {
  fast();
  lli q;
  sieve();
  q = 1;
  while (q--) {
    lli a, b, c, d, dd;
    cin >> a >> b >> c >> d;
    for (int i = 0; i <= 100; i++) {
      dd = d;
      for (int j = 0; j <= 100; j++) {
        if (dd == b) return cout << b << "\n", 0;
        dd += c;
      }
      b += a;
    }
    cout << -1 << "\n", 0;
  }
}
