using static System.Math; using System.Linq; using System; public class Hello { static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); var a = Array.ConvertAll(line, long.Parse); line = Console.ReadLine().Trim().Split(' '); var s0 = line[0]; var t0 = int.Parse(line[1]); line = Console.ReadLine().Trim().Split(' '); var s1 = line[0]; var t1 = int.Parse(line[1]); if (s0 == s1) { if (s0 == "A") Same(a, t0, t1, 0, 1, 2); else if (s0 == "B") Same(a, t0, t1, 1, 0, 2); else Same(a, t0, t1, 2, 0, 1); } else { if ((s0 == "A" && s1 == "B") | (s0 == "B" && s1 == "A")) Diff(a, t0, t1, 0, 1, 2); else if ((s0 == "B" && s1 == "C") | (s0 == "C" && s1 == "B")) Diff(a, t0, t1, 2, 0, 1); else Diff(a, t0, t1, 0, 2, 1); } } static void Diff(long[] a, int t0, int t1, int p, int q, int r) { var root = new long[4]; root[0] = t0 + t1 - 1; root[1] = a[p] + a[q] - root[0]; root[2] = t0 + a[r] + a[q] - t1; root[3] = a[p] - t0 + a[r] + t1; Console.WriteLine(root.Min()); } static void Same(long[] a, int t0, int t1, int p, int q, int r) { var root1 = Abs(t0 - t1); var mint = Min(t0, t1); var maxt = Max(t0, t1); var minbc = Min(a[q], a[r]); var root2 = mint + minbc + a[p] - maxt; Console.WriteLine(Min(root1, root2)); } static void goWarshall(long[,] dp) { var n = dp.GetLength(0); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) dp[j, k] = Math.Min(dp[j, k], dp[j, i] + dp[i, k]); } }