from collections import deque xyz = [int(i) for i in input().split()] srt = input().split() end = input().split() abc = [[10**10]*2 for i in range(3)] idx = ord(srt[0])-ord("A") d = deque() abc[idx][0] = int(srt[1])-1 abc[idx][1] = xyz[idx] - int(srt[1]) d.append([idx,0]) d.append([idx,1]) while len(d): idx,pos = d.popleft() for i in range(3): if i == idx: continue if abc[i][pos] > abc[idx][pos]+1: abc[i][pos] = abc[idx][pos]+1 d.append([i,pos]) if abc[i][1-pos] > abc[idx][pos]+xyz[idx]: abc[i][1-pos] = abc[idx][pos]+xyz[idx] d.append([i,1-pos]) idxend = ord(end[0])-ord("A") ans = min(int(end[1]) + abc[idxend][0] - 1,xyz[idxend] - int(end[1]) + abc[idxend][1]) if idx == idxend: ans = min(ans,abs(int(srt[0])-int(srt[1]))) print(ans)