local a, b, c = io.read("*n", "*n", "*n", "*l") local str = io.read() local start_type, start_idx = str:match("(%w) (%d+)") start_idx = tonumber(start_idx) str = io.read() local end_type, end_idx = str:match("(%w) (%d+)") end_idx = tonumber(end_idx) local edge = {} local function add(i1, i2, c) table.insert(edge, {i1, i2, c}) table.insert(edge, {i2, i1, c}) end add(1, 2, a - 1) add(3, 4, b - 1) add(5, 6, c - 1) add(1, 3, 1) add(1, 5, 1) add(3, 5, 1) add(2, 4, 1) add(2, 6, 1) add(4, 6, 1) if start_type == "A" then add(7, 1, start_idx - 1) add(7, 2, a - start_idx) elseif start_type == "B" then add(7, 3, start_idx - 1) add(7, 4, b - start_idx) else add(7, 5, start_idx - 1) add(7, 6, c - start_idx) end if end_type == "A" then add(8, 1, end_idx - 1) add(8, 2, a - end_idx) elseif end_type == "B" then add(8, 3, end_idx - 1) add(8, 4, b - end_idx) else add(8, 5, end_idx - 1) add(8, 6, c - end_idx) end local len = {} local inf = 10000000000 for i = 1, 8 do len[i] = inf end len[7] = 0 for i = 1, 10 do for j = 1, #edge do local src, dst, c = edge[j][1], edge[j][2], edge[j][3] if len[src] + c < len[dst] then len[dst] = len[src] + c end end end print(len[8])