#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
const long long int MAXN = 2e5 + 5;
const long long int mod = 1000000007;
const long long int N = 200005;
void solve() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  map<int, int> mp;
  int dis1 = b, dis2 = d;
  while (dis1 <= 1e6) {
    mp[dis1] += 1;
    dis1 += a;
  }
  while (dis2 <= 1e6) {
    mp[dis2] += 1;
    dis2 += c;
  }
  for (auto [a, b] : mp) {
    if (b == 2) {
      cout << a << "\n";
      return;
    }
  }
  cout << -1 << "\n";
  return;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
