#include int main () { int b0 = 0; int c0 = 0; int b1 = 0; int c1 = 0; int res = 0; int ans = 0; res = scanf("%d", &b0); res = scanf("%d", &c0); res = scanf("%d", &b1); res = scanf("%d", &c1); if (c0 < 0) { c0 += (1+(-c0)/b0)*b0; } if (c1 < 0) { c1 += (1+(-c1)/b1)*b1; } while (ans < b0*b1) { if (ans%b0 == c0%b0 && ans%b1 == c1%b1) { printf("%d\n", ans); return 0; } ans++; } printf("NaN\n"); return 0; }