#include <bits/stdc++.h>
int v[1000001];
using namespace std;
int main() {
  int a, b, c, d;
  scanf("%d%d%d%d", &a, &b, &c, &d);
  while (b <= 1000000) {
    v[b] = 1;
    b += a;
  }
  while (d <= 1000000) {
    if (v[d] == 1) {
      printf("%d", d);
      return 0;
    }
    d += c;
  }
  printf("-1");
  return 0;
}
