#include <bits/stdc++.h>
using namespace std;
int a, b, c, d;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  scanf("%d%d%d%d", &a, &b, &c, &d);
  if (b < d) {
    swap(b, d);
    swap(a, c);
  }
  int xx = gcd(a, c);
  if ((b - d) % xx != 0) {
    printf("-1\n");
  } else
    for (int i = 0;; i++) {
      if ((a * i + (b - d)) % c == 0) {
        printf("%d\n", b + a * i);
        break;
      }
    }
}
