#include <bits/stdc++.h>
using namespace std;
int INF = 2147483647;
int inf = -2147483648;
const double PI = acos(-1.0);
const double pi = 2 * asin(1.0);
int vis[1000009];
int vi[1000009];
int main() {
  ios::sync_with_stdio(false);
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  for (int i = 0; i <= 100; i++) {
    vis[b + a * i] = 1;
  }
  for (int i = 0; i <= 100; i++) {
    vi[d + c * i] = 1;
  }
  for (int i = 0; i < 1000009; i++) {
    if (vis[i] == 1 && vi[i] == 1) {
      cout << i << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
