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 CPP):

Solution:
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i, n) for ( int i = 0; i < n; i++ )
struct Point{int x, y;};

class Cube{
    public:
    char C[3];
    Cube(){ C[0] = 'W'; C[1] = 'R'; C[2] = 'B';}
    void setEmpty(){ C[0] = 'E';}
    void rotate(int d){
        if ( d % 2 == 0 ) swap(C[0], C[2]);
        else swap(C[0], C[1]);
    }
};

char target[3][3];

class Puzzle{
    public:
    Cube cubes[3][3];
    Point empty;
    int manhattanDist;
     
    Puzzle(){
	rep(y, 3) rep(x, 3) cubes[y][x] = Cube();
    }

    void setEmpty( int y, int x ){
        empty.x = x; empty.y = y;
        cubes[y][x].setEmpty();
    }

    void setmanhattanDist(){
        manhattanDist = 0;
	rep(y, 3) rep(x, 3) if ( cubes[y][x].C[0] != target[y][x] ) manhattanDist++;
    }

    void upDate( int y, int x, int d ){
        if ( target[y][x] != cubes[y][x].C[0] ) manhattanDist += d;
    }
};

int ix, iy, tx, ty, limit;
Puzzle u;
static const int dy[4] = {0, -1, 0, 1};
static const int dx[4] = {1, 0, -1, 0};

bool dfs( int depth, int pre, int seq ){
    if ( depth + u.manhattanDist-1 > limit ) return false;
    if ( u.manhattanDist == 0 ) return true;

    int x, y, nx, ny;

    y = u.empty.y;
    x = u.empty.x;

    Puzzle tmp = u;

    for ( int d = 0; d < 4; d++ ){
        if ( pre != -1 && d == (pre+2)%4 ) continue;
        ny = y + dy[d];
        nx = x + dx[d];
        if ( ny < 0 || nx < 0 || 3 <= nx || 3 <= ny ) continue;
        u = tmp;
        u.upDate(ny, nx, -1);
        u.upDate(y, x, -1);
        u.cubes[ny][nx].rotate(d);
        u.cubes[y][x] = u.cubes[ny][nx];
        u.setEmpty(ny, nx);
        u.upDate(ny, nx, 1);
        u.upDate(y, x, 1);

        if ( dfs( depth+1, d, seq) ) return true;
    }

    return false;
}

int compute(){
    Puzzle initial = Puzzle();
    initial.setEmpty(iy, ix);
    initial.setmanhattanDist();
    int start = initial.manhattanDist -1;
    while( start % 3 != 0 ) start--;
    for ( limit = start; limit <= 30; limit+=3 ){
        u = initial;
        if ( dfs(  0, -1, 0 ) ) {
            if ( limit == 0 ) return 0;
            limit -= 2;
            u = initial;
            if ( dfs(0, -1, 0 ) ) {
		return limit;
            } else {
		limit++;
                u = initial;
                if ( dfs(0, -1, 0) ) return limit;
                else return limit+1;
            }
            return 0;
        }
    }
    return -1;
}

int main(){
    while(1){
	cin >> ix >> iy;
	if ( ix == 0 && iy == 0 ) return false;
	ix--; iy--;
	rep(y, 3) rep(x, 3){
	    cin >> target[y][x];
	    if ( target[y][x] == 'E' ){ tx = x; ty = y; }
	}
	cout << compute() << endl;
    }
}