#include using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } int main() { fast_io(); int b0, c0, b1, c1; cin >> b0 >> c0 >> b1 >> c1; c0 = (c0 % b0 + b0) % b0; c1 = (c1 % b1 + b1) % b1; for (int i = 0; i < b0 * b1; i++) { if (i % b0 == c0 && i % b1 == c1) { cout << i << endl; return 0; } } cout << "NaN" << endl; }