#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  while (b != d && b < 100000) {
    if (b < d) b += a;
    if (d < b) d += c;
  }
  if (b != d)
    cout << -1;
  else
    cout << b;
}
