#include int main() { using namespace std; long B_0, C_0, B_1, C_1; cin >> B_0 >> C_0 >> B_1 >> C_1; for(unsigned long i{}; i <= 10000; ++i)if((i - C_0) % B_0 == 0 && (i - C_1) % B_1 == 0){ cout << i << endl; return 0; } cout << "NaN" << endl; return 0; }