#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, a1[100001], b1[100001];
string second, s1;
int main() {
  cin >> a >> c >> b >> d;
  a1[1] = c;
  b1[1] = d;
  for (int i = 2; i <= 100000; i++) a1[i] = c + i * a - a;
  for (int i = 2; i <= 100000; i++) {
    b1[i] = d + i * b - b;
  }
  for (int i = 1; i <= 1000; i++) {
    for (int j = 1; j <= 1000; j++)
      if (!(a1[j] - b1[i])) {
        cout << a1[j];
        return 0;
      }
  }
  cout << -1;
}
