#include <bits/stdc++.h>
int min(int x, int y) {
  if (x < y)
    return x;
  else
    return y;
}
int main() {
  int a, b, c, d;
  int i, j, n, m;
  scanf("%d%d", &a, &b);
  scanf("%d%d", &c, &d);
  n = b;
  m = d;
  i = 1;
  j = 1;
  int flag = 0xFFFFFFF;
  for (i = 0; i <= 1000; i++) {
    for (j = 0; j <= 1000; j++) {
      if (b + a * i == d + c * j) {
        flag = min(flag, b + a * i);
      }
    }
  }
  if (flag == 0xFFFFFFF)
    printf("-1\n");
  else
    printf("%d\n", flag);
}
