#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long solve(long long a, long long b, long long c, long long d) {
  if ((d - b) % gcd(a, c) != 0) return -1;
  for (long long y = 0; y <= 100; y++) {
    int x = (d - b + c * y) / a;
    if (x >= 0 and b + a * x == d + c * y) return b + a * x;
  }
}
int main() {
  int a, b, c, d;
  while (scanf("%d %d %d %d", &a, &b, &c, &d) == 4) {
    printf("%lld\n", solve(a, b, c, d));
  }
  return 0;
}
