#include <bits/stdc++.h>
using namespace std;
const long long MOD9 = 1e9 + 7;
const long long MAX = 1e7 + 10;
const double eps = 0.99;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {-1, 1, 0, 0};
int t, n, m, q, k;
bool s[MAX];
int main() {
  ios_base::sync_with_stdio(false);
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  for (long long i = 0; i < 100001; i++) s[b + i * a] = 1;
  for (long long i = 0; i < 100001; i++)
    if (s[d + i * c]) return cout << d + i * c, 0;
  cout << -1;
}
