#include <bits/stdc++.h>
using namespace std;
const int len = 1000;
int arr[len] = {0};
int m = 0;
bool is_appear(int x) {
  for (int i = 0; i < m - 1; ++i) {
    if (x == arr[i]) return true;
  }
  return false;
}
int main() {
  int a, b, c, d, h, ok = 1, j;
  cin >> a >> b >> c >> d;
  for (j = 0;; ++j) {
    if (a * j + b - d >= 0) break;
  }
  for (int i = j;; ++i) {
    h = (a * i + b - d) % c;
    if (h == 0) {
      cout << a * i + b << endl;
      break;
    } else {
      arr[m++] = h;
      if (is_appear(h)) {
        ok = 0;
        break;
      }
    }
  }
  if (ok == 0) cout << "-1" << endl;
  return 0;
}
