#include <bits/stdc++.h>
int main() {
  int a;
  int b;
  int c;
  int d;
  int i;
  int j;
  int time = -1;
  scanf("%d %d %d %d", &a, &b, &c, &d);
  for (i = 0; i < 101; i++) {
    for (j = 0; j < 101; j++) {
      if ((b + i * a) == (d + j * c) & (time == -1)) time = (b + i * a);
    }
  }
  printf("%d\n", time);
  return 0;
}
