#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 7;
const int INF = 1e9 + 7;
int n;
int main() {
  int a, b, c, d;
  scanf("%d%d%d%d", &a, &b, &c, &d);
  int flag = INF;
  for (int i = 0; i <= 1000; i++) {
    for (int j = 0; j <= 1000; j++)
      if (b + i * a == d + j * c) {
        flag = min(flag, b + i * a);
        break;
      }
  }
  if (flag == INF) flag = -1;
  printf("%d\n", flag);
}
