#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T BMOD(T p, T e, T m) {
  T ret = 1;
  while (e) {
    if (e & 1) ret = (ret * p) % m;
    p = (p * p) % m;
    e >>= 1;
  }
  return (T)ret;
}
template <class T>
inline T MODINV(T a, T m) {
  return BMOD(a, m - 2, m);
}
template <class T>
inline T isPrime(T a) {
  for (T i = 2; i <= sqrt(double(a)); i++) {
    if (a % i == 0) {
      return 0;
    }
  }
  return 1;
}
template <class T>
inline T lcm(T a, T b) {
  return (a / __gcd(a, b)) * b;
}
template <class T>
inline T power(T a, T b) {
  return (b == 0) ? 1 : a * power(a, b - 1);
}
template <class T>
inline string toString(T t) {
  stringstream ss;
  ss << t;
  return ss.str();
}
template <class T>
inline long long toLong(T t) {
  stringstream ss;
  ss << t;
  long long ret;
  ss >> ret;
  return ret;
}
template <class T>
inline int toInt(T t) {
  stringstream ss;
  ss << t;
  int ret;
  ss >> ret;
  return ret;
}
template <class T>
inline bool leapYear(T t) {
  if (!(t % (T)400) || (!(t % (T)4) && t % (T)100)) return true;
  return false;
}
string buffer;
int INT() {
  getline(cin, buffer);
  return toInt(buffer);
}
int LONG() {
  getline(cin, buffer);
  return toLong(buffer);
}
const int MX = 200010;
const long long MD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long a, b, c, d;
  cin >> a >> b;
  cin >> c >> d;
  long long Ans = 1e12;
  for (long long i = 0; i < 1000000; i++) {
    long long l = 0, r = 1e6;
    long long x = b + i * a;
    while (l <= r) {
      long long m = (l + r) / 2ll;
      long long y = d + m * c;
      if (x == y) {
        Ans = min(Ans, x);
        break;
      }
      if (x < y)
        r = m - 1;
      else
        l = m + 1;
    }
  }
  if (Ans == 1e12)
    cout << -1 << endl;
  else
    cout << Ans << endl;
  return 0;
}
