fn egcd(a: i64, b: i64) -> (i64, i64) { if b == 0 { return (1, 0); } let (x, y) = egcd(b, a % b); (y, x - a / b * y) } fn main() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let n: Vec = s.split_whitespace().flat_map(str::parse).collect(); let g = egcd(n[0], n[1]); let l = n[0] * n[1]; println!("{}", ((g.0 * n[0] * n[3] + g.1 * n[1] * n[2]) % l + l) % l); }