#include <bits/stdc++.h>
using namespace std;
int a, b, c, d;
int ans;
int main() {
  scanf("%d%d%d%d", &a, &b, &c, &d);
  ans = -1;
  for (int i = 0; i <= 999; ++i)
    for (int j = 0; j <= 999; ++j)
      if (b + a * i == d + c * j)
        ans = min(ans == -1 ? 1000000000 : ans, b + a * i);
  printf("%d\n", ans);
  return 0;
}
