#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 p = 1e9; // x -= p * b1 / g; // y -= p * b0 / g; // } { ll s = b0 * x + c0; ll t = b1 * y + c1; ll lcm = b1 * b0 / g; // s + lcm*p >= 0 // t + lcm*p >= 0 ll p = 0; if (s < 0) { p = max(p, (lcm - 1 - s) / lcm); } if (t < 0) { p = max(p, (lcm - 1 - t) / lcm); } x += p * b1 / g; y += p * b0 / g; // print(p, x, y); } { ll s = b0 * x + c0; ll t = b1 * y + c1; assert(s == t); assert(s >= 0); print(s); } return 0; }