#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int flag = 1;
  for (int i = 0; i < 100; i++) {
    int lhs = a * i + b;
    int j = (lhs - d) / c;
    if (j < 0) continue;
    int rhs = c * j + d;
    if (lhs == rhs) {
      flag = 0;
      cout << lhs << endl;
      break;
    }
  }
  if (flag) cout << -1 << endl;
  return 0;
}
