fn main() { let mut xx = String::new(); std::io::stdin().read_line(&mut xx).ok(); let xx: Vec = xx.split_whitespace().flat_map(str::parse).collect(); let (p, q, r, k) = (xx[0], xx[1], xx[2], xx[3]); let mut seq = vec![p % 10, q % 10, r % 10]; loop { let last3 = &seq[seq.len() - 3..]; if seq.windows(3).position(|x| x == last3).unwrap() < seq.len() - 3 { seq.resize(seq.len() - 3, 0); break; } seq.push((last3[0] + last3[1] + last3[2]) % 10); } println!("{}", seq[(k - 1) % seq.len()]); }