using System.Linq; using System; public class Hello { static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); var a = Array.ConvertAll(line, int.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, 1, 2, 0); else Diff(a, t0, t1, 0, 2, 1); } } static void Diff(int[] a, int t0, int t1, int p, int q, int r) { var root = new long[4]; root[0] = (t0 - 1L) + (t1 - 1L) + 1L; root[1] = (a[p] - t0) + (a[q] - t1) + 1L; root[2] = (t0 - 1L) + (a[r] - 1L) + 2L + (a[q] - t1); root[3] = (a[p] - t0) + (a[r] - 1L) + 2L + (t1 - 1L); Console.WriteLine(root.Min()); } static void Same(int[] a, int t0, int t1, int p, int q, int r) { if (t0 > t1) { Same(a, t1, t0, p, q, r); return; } var root = new long[3]; root[0] = t1 - t0; root[1] = (t0 - 1L) + (a[q] + 1L) + (a[p] - t1); root[2] = (t0 - 1L) + (a[r] + 1L) + (a[p] - t1); Console.WriteLine(root.Min()); } }