#include using namespace std; typedef long long ll; #define int ll #define _FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define all(x) x.begin() , x.end() #define pii pair #define pb emplace_back #define F first #define S second const int mod = 1e9 + 7; const int MAXX = 1e5 + 5; int a , b , c , d; signed main() { _FastIO; cin >> b >> a >> d >> c; if(a < 0) a = b + a; if(c < 0) c = d + c; for(int i = 0; i <= 200000000; i++){ if((i % b == a) && (i % d == c)){ cout << i << endl; return 0; } } cout << "NaN" << endl; return 0; }