#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d;
const int maxn = 10000000;
long long m[maxn], n[maxn];
int main() {
  cin >> a >> b;
  cin >> c >> d;
  for (int i = 0; i < maxn; i++) {
    m[i] = b + i * a;
    n[i] = d + i * c;
  }
  int i = 0, j = 0;
  while (1) {
    while (m[i] < n[j]) {
      i++;
    }
    if (i >= maxn) break;
    while (n[j] < m[i]) j++;
    if (j >= maxn) break;
    if (m[i] == n[j]) {
      cout << m[i] << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
