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:
import java.util.*;
import java.awt.geom.*;

import static java.lang.Math.*;
public class Main {
	final Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		new Main().init();
	}
	void init(){
		new AOJ1268();
	}
	class AOJ1268{
		AOJ1268(){
			while(sc.hasNext()){
				initEmpX = sc.nextInt();
				initEmpY = sc.nextInt();
				if((initEmpX|initEmpY)==0)	break;
				--initEmpX;
				--initEmpY;
				solve();
			}
		}
		int initEmpX,initEmpY;
		int[] target, vtd;
		final int MAX = Integer.parseInt("1000000000", 7);
		final int[] vx={0,1,0,-1}, vy={-1,0,1,0};
		final int[][] dic = {{0,0,0,0}, {0,0,1,2}, {0,3,0,4}, {0,5,6,0}};
		void solve(){
			target = new int[9];
			for(int i=0; i<9; i++){
				String in=sc.next();
				int c=0;
				if(in.equals("W"))		c=1;
				else if(in.equals("B"))	c=2;
				else if(in.equals("R"))	c=3;
				target[i] = c;
			}
			
			int lower = -1, upper = 31, mid = 0;
			while(lower+1 < upper){
				mid = (lower+upper) / 2;
				int[] init = {1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3,1,3};
				init[(initEmpX+initEmpY*3)*2] = 0;
				init[(initEmpX+initEmpY*3)*2+1] = 0;
				if( DFS(init, 0, mid, initEmpX, initEmpY, -1))	upper = mid;
				else	lower = mid;
			}
			System.out.println( upper>30? -1 : upper);
		}
		
		boolean DFS(int[] dice, int depth, int lim, int ex, int ey, int lpos){
			int diff=0;
			for(int i=0; i<9; ++i)if( dice[i*2] != target[i] )	diff += 1;
			if(diff == 0)	return true;
			
			if(depth+diff-1 > lim)	return false;
			
			int epos = ex+ey*3;
			for(int v=0; v<4; ++v){
				int xx = ex+vx[v], yy = ey+vy[v];
				if( !(0<=xx && xx<3 && 0<=yy && yy<3) )	continue;
				int pos = xx+yy*3;
				if(pos == lpos)	continue;
				
				int top = dice[pos*2], front = dice[pos*2+1],
					newTop = 0, newFront = 0;
				if(vx[v] != 0){
					newTop = 6-(top+front);
					newFront = front;
				}else{
					newTop = front;
					newFront = top;
				}
				
				dice[pos*2] = 0;
				dice[pos*2+1] = 0;
				dice[epos*2] = newTop;
				dice[epos*2+1] = newFront;
				
				if( DFS(dice, depth+1, lim, xx, yy, epos) )	return true;
				
				dice[pos*2] = top;
				dice[pos*2+1] = front;
				dice[epos*2] = 0;
				dice[epos*2+1] = 0;
			}
			return false;
		}
		int getHash(int[] dice){
			StringBuilder tmp = new StringBuilder();
			for(int i=0; i<18; i+=2)	tmp.append(dic[dice[i]][dice[i+1]]);
			return Integer.parseInt(tmp.toString(), 7);
		}
	}
}