#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e7 + 5;
const int nil = 0;
const int oo = (1 << 28);
bool g1[MAX], g2[MAX];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int a, b, c, d;
  cin >> b >> a >> d >> c;
  for (int i = a; i < MAX; i += b) g1[i] = true;
  for (int i = c; i < MAX; i += d) {
    if (g1[i]) {
      cout << i << "\n";
      return 0;
    }
  }
  cout << "-1\n";
  return 0;
}
