#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int ans = -1;
  for (int i = 0; i <= 1000; i++) {
    for (int j = 0; j <= 1000; j++) {
      if (b + i * a == d + j * c && (ans == -1 || b + i * a < ans)) {
        ans = b + i * a;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
