fn main() { let mut xyz = String::new(); std::io::stdin().read_line(&mut xyz).ok(); let xyz: Vec = xyz.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let x = xyz[0]; let y = xyz[1]; let z = xyz[2]; let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec<&str> = temp.trim().split_whitespace().collect(); let s0 = temp[0].chars().nth(0).unwrap(); let t0 = temp[1].parse::().unwrap(); let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec<&str> = temp.trim().split_whitespace().collect(); let s1 = temp[0].chars().nth(0).unwrap(); let t1 = temp[1].parse::().unwrap(); let mut result = 1isize << 60; let s0sswitch = t0; let s0eswitch = if s0 == 'A' { x } else if s0 == 'B' { y } else { z } + 1 - t0; let s1sswitch = t1; let s1eswitch = if s1 == 'A' { x } else if s1 == 'B' { y } else { z } + 1 - t1; let totals = vec![x-1, y-1, z-1]; for &middle in vec!['A', 'B', 'C'].iter() { if s0 == middle && middle == s1 { result = result.min((t0 - t1).abs()); } else if s0 == s1 { result = result.min(s0sswitch + totals[middle as usize - 'A' as usize] + s1eswitch); result = result.min(s0eswitch + totals[middle as usize - 'A' as usize] + s1sswitch); } else if s0 == middle { result = result.min(s0sswitch + s1sswitch - 1); result = result.min(s0eswitch + s1eswitch - 1); } else if s1 == middle { result = result.min(s0sswitch + s1sswitch - 1); result = result.min(s0eswitch + s1eswitch - 1); } else { result = result.min(s0sswitch + totals[middle as usize - 'A' as usize] + s1eswitch); result = result.min(s0eswitch + totals[middle as usize - 'A' as usize] + s1sswitch); } } println!("{}", result); }