use proconio::{fastout, input}; #[fastout] fn main() { input! { a: u16, b: u16, c: u16, d: u16, m: u16, } println!("{}", output(solve(a, b, c, d, m))); } fn solve(a: u16, b: u16, c: u16, d: u16, m: u16) -> u16 { if (a + c) / m != (b + d) / m { m - 1 } else { (b + d) % m } } fn output(ans: u16) -> u16 { ans }