#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
const long long MOD = 1e9 + 7;
bool isPrime(int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (int i = 5; i * i <= n; i += 6) {
    if (n % i == 0 || n % (i + 2) == 0) return false;
  }
  return true;
}
void solve() {
  int a, b, c, d;
  cin >> b >> a >> d >> c;
  for (int i = 0; i < 10000; i++) {
    if ((a + b * i - c) % d == 0 && (a + b * i - c) >= 0) {
      cout << a + b * i << "\n";
      return;
    }
  }
  cout << -1 << "\n";
  return;
}
int main() { solve(); }
