#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
  ios::sync_with_stdio(0);
  int a, b, c, d;
  scanf("%d %d", &a, &b);
  scanf("%d %d", &c, &d);
  for (int i = 0; i < 10000000; i++) {
    int k = b + a * i;
    if (k - d < 0) continue;
    if ((k - d) % c == 0) {
      printf("%d", k);
      return 0;
    }
  }
  printf("-1");
  return 0;
}
