#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:33554432")
using namespace std;
int a, b, c, d;
long long gcd(long long aa, long long bb) {
  if (bb == 0) return aa;
  return gcd(bb, aa % bb);
}
long long lca(long long aa, long long bb) { return aa * bb / gcd(aa, bb); }
int main() {
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);
  cin >> a >> b >> c >> d;
  int dd = max(b, d) - min(b, d);
  if ((a % c == 0 || c % a == 0) && dd % min(a, c) != 0) {
    cout << -1 << endl;
    return 0;
  }
  long long ans1 = b, ans2 = d;
  int cnrt = 0;
  while (ans1 != ans2 && cnrt < 100000) {
    if (ans1 < ans2) {
      ans1 += ((ans2 - ans1) / a) * a;
      if (ans1 < ans2) ans1 += a;
    }
    while (ans2 < ans1) {
      ans2 += ((ans1 - ans2) / c) * c;
      if (ans2 < ans1) ans2 += c;
    }
    cnrt++;
  }
  if (cnrt >= 100000)
    cout << -1 << endl;
  else
    cout << ans1 << endl;
  return 0;
}
