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