import Control.Applicative main :: IO () main = do xs <- words <$> getContents putStrLn $ show $ solve xs solve :: [String] -> Integer solve [x, y, z, startRosen, start, endRosen, end] = f (read x, read y, read z, startRosen, read start, endRosen, read end) where f :: (Integer, Integer, Integer, String, Integer, String, Integer) -> Integer f (a, b, c, s1, st, s2, ed) | s1 == s2 = g (s1, a, b, c, st, ed) | otherwise = h (s1, s2, a, b, c, st, ed) g :: (String, Integer, Integer, Integer, Integer, Integer) -> Integer g (s, a, b, c, start, end) = minimum xs where xs = case s of "A" -> [abs (end - start), (start - 1) + 1 + (b - 1) + 1 + (a - end), (start - 1) + 1 + (c - 1) + 1 + (a - end)] "B" -> [abs (end - start), (start - 1) + 1 + (a - 1) + 1 + (b - end), (start - 1) + 1 + (c - 1) + 1 + (b - end)] "C" -> [abs (end - start), (start - 1) + 1 + (a - 1) + 1 + (c - end), (start - 1) + 1 + (b - 1) + 1 + (c - end)] h :: (String, String, Integer, Integer, Integer, Integer, Integer) -> Integer h (s1, s2, a, b, c, start, end) = minimum xs where xs = case (s1, s2) of ("A", "B") -> [(a - start) + 1 + (c - 1) + 1 + (end - 1), (a - start) + 1 + (b - end), (start - 1) + 1 + (end - 1), (start - 1) + 1 + (c - 1) + 1 + (b - end)] ("A", "C") -> [(a - start) + 1 + (b - 1) + 1 + (end - 1), (a - start) + 1 + (c - end), (start - 1) + 1 + (end - 1), (start - 1) + 1 + (b - 1) + 1 + (c - end)] ("B", "C") -> [(b - start) + 1 + (a - 1) + 1 + (end - 1), (b - start) + 1 + (c - end), (start - 1) + 1 + (end - 1), (start - 1) + 1 + (a - 1) + 1 + (c - end)] ("B", "A") -> [(b - start) + 1 + (c - 1) + 1 + (end - 1), (b - start) + 1 + (a - end), (start - 1) + 1 + (end - 1), (start - 1) + 1 + (c - 1) + 1 + (a - end)] ("C", "A") -> [(c - start) + 1 + (b - 1) + 1 + (end - 1), (c - start) + 1 + (a - end), (start - 1) + 1 + (end - 1), (start - 1) + 1 + (b - 1) + 1 + (a - end)] ("C", "B") -> [(c - start) + 1 + (a - 1) + 1 + (end - 1), (c - start) + 1 + (b - end), (start - 1) + 1 + (end - 1), (start - 1) + 1 + (a - 1) + 1 + (b - end)]