#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d, k = 1111;
  set<int> s1, s2;
  cin >> a >> b >> c >> d;
  b -= a, d -= c;
  while (k--) {
    b += a;
    d += c;
    s1.insert(b);
    s2.insert(d);
    if (s1.count(d)) return cout << d, 0;
    if (s2.count(b)) return cout << b, 0;
  }
  return cout << -1, 0;
}
