#include <bits/stdc++.h>
using namespace std;
int a, b, c, d;
map<int, int> m;
void nhap() { cin >> a >> b >> c >> d; }
void solve() {
  m[b] = 1;
  for (int i = 1; i <= 100000; i++) b += a, m[b] = 1;
  for (int i = 1; i <= 100000; i++)
    if (m[d]) {
      cout << d;
      return;
    } else
      d += c;
  cout << -1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  nhap();
  solve();
  return 0;
}
