#include // clang-format off using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair; const ll INF=4e18; void print0(){}; template void print0(H h,T... t){cout<void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);} void perr0(){}; template void perr0(H h,T... t){cerr<void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);} void ioinit() { cout<> b0 >> c0 >> b1 >> c1; c0 %= b0; if (c0 < 0) c0 += b0; c1 %= b1; if (c1 < 0) c1 += b1; ll g = gcdf(b0, b1); if (c0 > c1) { swap(b0, b1); swap(c0, c1); } ll dif = c1 - c0; if (dif % g != 0) { print("NaN"); return 0; } ll x, y; extgcd(b0, b1, x, y); y = -y; x *= dif / g; y *= dif / g; { ll s = b0 * x + c0; ll t = b1 * y + c1; // s + (b1/g)*p >= 0 // t + (b0/g)*p >= 0 ll p = 0; if (s < 0) { ll d1 = b1 / g; p = max(p, (d1 - 1 - s) / d1); } if (t < 0) { ll d0 = b0 / g; p = max(p, (d0 - 1 - t) / d0); } x += p * b1 / g; y += p * b0 / g; } { ll s = b0 * x + c0; ll t = b1 * y + c1; print(s); } return 0; }