#[allow(dead_code)] pub mod ut { #[macro_export] macro_rules! input { ( string ) => {{ let mut buf = String::new(); std::io::stdin() .read_line(&mut buf) .map(|_| buf.trim().to_string()) .unwrap() }}; ( [ $t:ty ] ) => {{ input!(string) .split_ascii_whitespace() .map(|x| x.parse::<$t>().unwrap()) .collect::>() }}; ( ( $($t:ty ),* ) ) => {{ let buf = input!(string); let mut it = buf.split_ascii_whitespace(); ( $( it.next().map(|x| x.parse::<$t>().unwrap() ).unwrap(), )* ) }}; ( $($x:tt),* ) => {{ { ( $( input!($x) ),* ) } }}; } } fn main() { let (a, b, c, d, m) = input! {(usize, usize, usize, usize, usize)}; let ans = { let mut ans = 0usize; for ix in a..=b { for iy in c..=d { ans = std::cmp::max(ans, (ix + iy) % m); } } ans }; println!("{}", ans); }