#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  int a, b, c, d;
  scanf("%d", &a);
  scanf("%d", &b);
  scanf("%d", &c);
  scanf("%d", &d);
  int k = gcd(a, c);
  if (abs(b - d) % k != 0) {
    cout << "-1";
    return 0;
  }
  int y1 = min(b, d), y2 = max(b, d), flag = 0, flag1 = 0;
  if (y1 == b) flag1 = 1;
  if (y1 == y2) flag = 1;
  while (flag == 0) {
    if (flag1 == 1)
      y1 += a;
    else
      y1 += c;
    while (y2 < y1) {
      if (flag1 == 1)
        y2 += c;
      else
        y2 += a;
    }
    if (y1 == y2) flag = 1;
  }
  cout << y1;
}
