#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  for (int i = 0; i <= 100000; i++) {
    if ((b + i * a - d) >= 0 && (b + i * a - d) % c == 0) {
      cout << b + i * a << endl;
      return 0;
    }
  }
  for (int i = 0; i <= 100000; i++) {
    if ((d + i * c - b) >= 0 && (d + i * c - b) % a == 0) {
      cout << d + i * c << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
