#include <bits/stdc++.h>
using namespace std;
map<long long, int> pr;
int a, b, c, d;
int main() {
  scanf("%d %d", &a, &b);
  scanf("%d %d", &c, &d);
  for (int i = 0; i < 200000; ++i) {
    pr[b + a * i]++;
    pr[d + c * i]++;
    if (pr[d + c * i] == 2 || pr[b + a * i] == 2) {
      printf("%d", pr[d + c * i] == 2 ? d + c * i : b + a * i);
      return 0;
    }
  }
  printf("-1");
  return 0;
}
