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