#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  if (abs(b - d) % gcd(a, c)) {
    cout << -1;
    return 0;
  }
  int x = b;
  while (1) {
    if ((x - d) >= 0 && (x - d) % c == 0) {
      cout << x;
      return 0;
    }
    x += a;
  }
}
