#define _USE_MATH_DEFINES #include //cin, cout #include //vector #include //sort,min,max,count #include //string,getline, to_string #include //abs(int) #include //swap, pair #include //deque #include //INT_MAX #include //bitset #include //sqrt, ceil. M_PI, pow, sin #include //fixed #include //setprecision #include //stringstream #include //gcd, assumlate #include //randam_device #include //numeric_limits using namespace std; constexpr long long int D_MOD = 1000000007; int main() { long long int A, B, a, b; cin >> A >> B >> a >> b; long long int x = 0; while (1) { if ((x - a) % A == 0) { if ((x - b) % B == 0) { cout << x << endl; return 0; } } x++; } return 0; }