#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int maxn = 1000000 + 5;
bool vis[maxn];
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  vis[b] = true;
  for (int i = 1; b <= maxn - 5; i++) {
    vis[b] = true;
    b += a;
  }
  bool flag = false;
  for (int i = 1; d <= maxn - 5; i++) {
    if (vis[d]) {
      flag = true;
      break;
    }
    d += c;
  }
  if (flag)
    printf("%d\n", d);
  else
    printf("-1\n");
  return 0;
}
