import sequtils,strutils,strscans var xyz = stdin.readline.split.map(parseInt) S0, S1: string s0, s1, t0, t1, sd1, sdz, d1z, gd1, gdz, dsg,ans : int discard scanf(stdin.readline,"$w $i",S0, t0) discard scanf(stdin.readline,"$w $i",S1, t1) s0 = S0[0].ord - 'A'.ord s1 = S1[0].ord - 'A'.ord sd1 = t0 - 1 sdz = xyz[s0] - t0 d1z = min(xyz) gd1 = t1 - 1 gdz = xyz[s1] - t1 if s1 == s0: dsg = abs(t0 - t1) else: dsg = int.high div 2 ans = dsg sd1 = min(sdz + d1z + 1, sd1 + 1) sdz = min(sdz + 1, d1z + sd1) gd1 = min(gdz + d1z, gd1) gdz = min(gdz, d1z + gd1) ans = min(ans, sd1 + gd1) ans = min(ans, sdz + gdz) echo ans