import java.io.*; import java.util.*; import java.math.*; // import java.awt.Point; public class Main { InputStream is; PrintWriter out; String INPUT = ""; // static int mod = 1_000_000_007; int mod = 998244353; // long inf = Long.MAX_VALUE/2; int inf = Integer.MAX_VALUE/2; void solve(){ int x = ni(); int y = ni(); int z = ni(); String[] s1 = ns().split(" "); String[] s2 = ns().split(" "); int idx1 = set_idx(s1[0], Integer.parseInt(s1[1])-1, x, y, z); int idx2 = set_idx(s2[0], Integer.parseInt(s2[1])-1, x, y, z); ArrayDeque q = new ArrayDeque<>(); q.add(new int[]{idx1, 0}); boolean[] seen = new boolean[x+y+z]; while(true){ int[] cur = q.pop(); int id = cur[0]; int dist = cur[1]; seen[id] = true; if(id==idx2){ out.println(dist); return; } if(id==0){ if(!seen[1]) q.add(new int[]{1,dist+1}); if(!seen[x]) q.add(new int[]{x,dist+1}); if(!seen[x+y]) q.add(new int[]{x+y,dist+1}); } if(id==x){ if(!seen[0]) q.add(new int[]{0,dist+1}); if(y!=1&&!seen[x+1]) q.add(new int[]{x+1,dist+1}); if(!seen[x+y]) q.add(new int[]{x+y,dist+1}); } if(id==x+y){ if(!seen[0]) q.add(new int[]{0,dist+1}); if(!seen[x]) q.add(new int[]{x,dist+1}); if(z!=1&&!seen[x+y+1]) q.add(new int[]{x+y+1,dist+1}); } if(id==x-1){ if(x!=1&&!seen[x-2]) q.add(new int[]{x-2,dist+1}); if(!seen[x+y-1]) q.add(new int[]{x+y-1,dist+1}); if(!seen[x+y+z-1]) q.add(new int[]{x+y+z-1,dist+1}); } if(id==x+y-1){ if(!seen[x-1]) q.add(new int[]{x-1,dist+1}); if(y!=1&&!seen[x+y-1]) q.add(new int[]{x+y-2,dist+1}); if(!seen[x+y+z-1]) q.add(new int[]{x+y+z-1,dist+1}); } if(id==x+y+z-1){ if(!seen[x-1]) q.add(new int[]{x-1,dist+1}); if(!seen[x+y-1]) q.add(new int[]{x+y-1,dist+1}); if(z!=1&&!seen[x+y+z-2]) q.add(new int[]{x+y+z-2,dist+1}); } if(id!=0&&id!=x&&id!=x+y&&id!=x-1&&id!=x+y-1&&id!=x+y+z-1){ if(!seen[id-1]) q.add(new int[]{id-1,dist+1}); if(!seen[id+1]) q.add(new int[]{id+1,dist+1}); } } } int set_idx(String s, int idx, int x, int y, int z){ if(s.equals("A")){ return idx; } else if(s.equals("B")){ return x + idx; } else{ return x + y + idx; } } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b) && b != ' ')){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }