#include #include using namespace std; using namespace boost::integer; int main() { int A, B, a, b; cin >> A >> B >> a >> b; auto res = extended_euclidean(A, B); int ans = ((A * res.x) * (b - a) + a) % (A * B); ans = (ans + (A * B)) % (A * B); cout << ans << endl; }