#include <bits/stdc++.h>
using namespace std;
int rick, morty;
int a, b, c, d, x, y;
int main() {
  cin >> a >> b >> c >> d;
  for (x = 0, y = 0; x <= 100 && y <= 100;) {
    rick = b + x * a;
    morty = d + y * c;
    if (rick == morty) {
      cout << rick;
      return 0;
    }
    if (rick > morty)
      y++;
    else
      x++;
  }
  cout << -1;
  return 0;
}
