#include <bits/stdc++.h>
using namespace std;
long long exgcd(long long a, long long b, long long &x, long long &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  } else {
    long long d = exgcd(b, a % b, x, y);
    long long tmp = x;
    x = y;
    y = tmp - a / b * y;
    return d;
  }
}
long long Rand() { return 1LL * rand() * rand(); }
int main() {
  long long A, B, C, D, x, y;
  long long a, b, c, d;
  cin >> A >> B >> C >> D;
  srand(time(0));
  a = A;
  b = -C;
  c = D - B;
  long long gd = exgcd(a, b, x, y);
  if (c % gd != 0) {
    cout << "-1" << endl;
    return 0;
  }
  x = x * c / gd;
  y = y * c / gd;
  long long p = b / gd;
  x = (x % p + p) % p;
  if (x < 0) {
    x += abs(p);
  }
  while ((c - a * x) / b < 0) {
    x += abs(p);
  }
  cout << B + A * x << endl;
  return 0;
}
