Problem: Let's play a puzzle using eight cubes placed on a 3 × 3 board leaving one empty square.

Faces of cubes are painted with three colors. As a puzzle step, you can roll one of the cubes to the adjacent empty square. Your goal is to make the specified color pattern visible from above by a number of such steps.

The rules of this puzzle are as follows.

1. Coloring of Cubes: All the cubes are colored in the same way as shown in Figure 3. The opposite faces have the same color.

<image>

Figure 3: Coloring of a cube



2. Initial Board State: Eight cubes are placed on the 3 × 3 board leaving one empty square. All the cubes have the same orientation as shown in Figure 4. As shown in the figure, squares on the board are given x and y coordinates, (1, 1), (1, 2), .. ., and (3, 3). The position of the initially empty square may vary.

<image>

Figure 4: Initial board state



3. Rolling Cubes: At each step, we can choose one of the cubes adjacent to the empty square and roll it into the empty square, leaving the original position empty. Figure 5 shows an example.

<image>

Figure 5: Rolling a cube



4. Goal: The goal of this puzzle is to arrange the cubes so that their top faces form the specified color pattern by a number of cube rolling steps described above.



Your task is to write a program that finds the minimum number of steps required to make the specified color pattern from the given initial state.



Input

The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets is less than 16. Each dataset is formatted as follows.


x y
F11 F21 F31
F12 F22 F32
F13 F23 F33


The first line contains two integers x and y separated by a space, indicating the position (x, y) of the initially empty square. The values of x and y are 1, 2, or 3.

The following three lines specify the color pattern to make. Each line contains three characters F1j, F2j, and F3j, separated by a space. Character Fij indicates the top color of the cube, if any, at position (i, j) as follows:


B: Blue
W: White
R: Red
E: the square is Empty.


There is exactly one 'E' character in each dataset.

Output

For each dataset, output the minimum number of steps to achieve the goal, when the goal can be reached within 30 steps. Otherwise, output "-1" for the dataset.

Example

Input

1 2
W W W
E W W
W W W
2 1
R B W
R W W
E W W
3 3
W B W
B R E
R B R
3 3
B W R
B W R
B E R
2 1
B B B
B R B
B R E
1 1
R R R
W W W
R R E
2 1
R R R
B W B
R R E
3 2
R R R
W E W
R R R
0 0


Output

0
3
13
23
29
30
-1
-1

Solution (in JAVA):

Solution:
//package C;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	Scanner sc;

	Main() {
		sc = new Scanner(System.in);
	}

	String turnR(int p, String d) {
		char[] c = d.toCharArray();
		int p2 = c[c.length - 1] - '0';
		if ('3' <= c[p * 2 + 1] && c[p * 2 + 1] <= '5') {
			c[p2 * 2] = ('3' <= c[p * 2] && c[p * 2] <= '5') ? (char) (c[p * 2] - 3)
					: (char) (c[p * 2] + 3);
		} else {
			c[p2 * 2] = c[p * 2];
		}
		c[p2 * 2 + 1] = c[p * 2 + 1];
		c[p * 2] = c[p * 2 + 1] = '6';
		c[c.length - 1] = (char) (p + '0');
		return new String(c);
	}

	String turnF(int p, String d) {
		char[] c = d.toCharArray();
		int p2 = c[c.length - 1] - '0';
		c[p2 * 2] = c[p * 2 + 1];
		c[p2 * 2 + 1] = c[p * 2];
		c[p * 2] = c[p * 2 + 1] = '6';
		c[c.length - 1] = (char) (p + '0');
		return new String(c);
	}

	String surface(String s) {
		char c[] = new char[9];
		for (int i = 0; i < 9; i++) {
			c[i] = s.charAt(i * 2);
		}
		return new String(c);
	}

	int equals(String d, int start) {
		int ret = 0;
		for (int i = 0; i < 9; i++) {
			int c1 = d.charAt(i*2);
			int c2 = d.charAt(i*2 + 1);
			if (start == i)
				continue;
			if(!(c1 == '0' || c2 == '1' || (c1 == '5' && c2=='5'))){
//				hm.put(d, false);
				ret++;
			}
//			if (c != '0' && c != '4' && c != '5')
//				return false;
		}
		return ret;
	}

	int map[];
	int diff(int a,int b){
		if(map[a]!=0){
			return -map[a]-1;
		}
		int z1 = a/3;
		int z2 = b/3;
		int z3 = a%3;
		int z4 = b%3;
		
		map[a] = -Math.abs(z1-z2)+Math.abs(z1-z2)-1;
		return Math.abs(z1-z2)+Math.abs(z3-z4);
	}
	void run() {
		for (;;) {
			map = new int[9];

			int x = sc.nextInt();
			int y = sc.nextInt();
			if (x + y == 0) {
				break;
			}
			x--;
			y--;
			String d = "";
			int ex = -1, ey = -1;
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 3; j++) {
					char in = sc.next().charAt(0);
					if (in == 'W') {
						d += '0';
						d += '3';
					} else if (in == 'R') {
						d += '1';
						d += '4';
					} else if (in == 'B') {
						d += '2';
						d += '5';
					} else {
						ex = j;
						ey = i;
						d += '6';
						d += '6';
					}
				}
			}
			d += (char) ((ex + ey * 3) + '0');

			HashSet<String> used = new HashSet<String>();
			LinkedList<String> q = new LinkedList<String>();
			q.add(d);
			used.add((d));
			boolean ok = false;
			for (int i = 0; i <= 30; i++) {
//				System.out.println(i);
				int size = q.size();
				for (int j = 0; j < size; j++) {
					String cur = q.poll();
					int sv = equals(cur, x + y * 3);
					if (sv == 0) {
						ok = true;
						break;
					}
					
					if(i == 30){
						continue;
					}
					if( i+sv  > 30) {
						continue;
					}
					int dx[] = { 0, 0, 1, -1 };
					int dy[] = { 1, -1, 0, 0 };
					int pos = cur.charAt(cur.length() - 1) - '0';
					int cx = pos % 3;
					int cy = pos / 3;
					for (int k = 0; k < 4; k++) {
						int nx = dx[k] + cx;
						int ny = dy[k] + cy;
						if (0 <= nx && nx < 3 && ny >= 0 && ny < 3) {
							String next;
							if (nx != cx) {
								// System.out.println(cur);
								next = turnR(nx + ny * 3, cur);
								// System.out.println(next);
							} else {
								// System.out.println(cur);
								next = turnF(nx + ny * 3, cur);
								// System.out.println(next);
							}
							if (!used.contains((next))) {
								used.add((next));
								q.add(next);
							}
						}
					}
				}
				if (ok) {
					System.out.println(i);
					break;
				}
			}
			if (!ok) {
				System.out.println(-1);
			}
		}
	}

	public static void main(String args[]) {
		Main m = new Main();
		m.run();
	}
}