#include #include #include #include #include #include using namespace std; int main(){ int b0,c0,b1,c1; cin >> b0 >> c0 >> b1 >> c1; while(c0<0) c0 += b0; while(c1<0) c1 += b1; c0 %= b0; c1 %= b1; for(int i = 0;i<1e6;i++){ if(i%b0==c0%b0){ if(i%b1==c1%b1){ cout << i << endl; return 0; } } } cout << "NaN" << endl; }