#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

// 0 ... B-W-B
// 1 ... R-W-R
// 2 ... W-B-W
// 3 ... R-B-R
// 4 ... W-R-W
// 5 ... B-R-B
// 6 ... empty

// 0 -> LR, 1 -> UD

int to[6][2] = {{2, 5}, {4, 3}, {0, 4}, {5, 1}, {1, 2}, {3, 0}};

int encode(int state[3][3]) {
  int emptyPos = 0;
  int res = 0;

  for (int i=0; i<3; i++) {
    for(int j=0; j<3; j++) {
      if (state[i][j] == 6)
        emptyPos = i * 3 + j;
      else
        res = res * 6 + state[i][j];
    }
  }

  return res * 9 + emptyPos;
}

void decode(int hash, int state[3][3]) {
  int emptyPos = hash % 9;
  hash /= 9;

  state[emptyPos/3][emptyPos%3] = 6;

  for (int i=2; i>=0; i--) {
    for (int j=2; j>=0; j--) {
      if (i * 3 + j == emptyPos) continue;
      state[i][j] = hash % 6;
      hash /= 6;
    }
  }
}

int memo[1679616 * 9]; //6^8 * 9

int base6[] = {1, 6, 36, 216, 1296, 7776, 46656, 279936, 1679616};

void print(int hash) {
  int state[3][3];
  decode(hash, state);
  for (int i=0; i<3; i++) {
    for (int j=0; j<3; j++)
      printf("%d ", state[i][j]);
    puts("");
  }
  puts("");
}

void bfs(int y, int x) {
  queue<int> que;
  int state[3][3];

  que.push(y * 3 + x);

  for(int i=0; i<=30; i++) {
    queue<int> que2;

    while(que.size()) {
      int hash = que.front();
      que.pop();

      if (memo[hash]) continue;

      memo[hash] = i + 1;

      decode(hash, state);

      int emptyX = hash % 9 % 3;
      int emptyY = hash % 9 / 3;

      if (emptyY > 0) {
        int tmp = state[emptyY-1][emptyX];
        state[emptyY][emptyX] = to[tmp][1];
        state[emptyY-1][emptyX] = 6;

        int tmp2 = encode(state);
        if (!memo[tmp2]) que2.push(tmp2);

        state[emptyY-1][emptyX] = tmp;
        state[emptyY][emptyX] = 6;
      }

      if (emptyY+1 < 3) {
        int tmp = state[emptyY+1][emptyX];
        state[emptyY][emptyX] = to[tmp][1];
        state[emptyY+1][emptyX] = 6;

        int tmp2 = encode(state);
        if (!memo[tmp2]) que2.push(tmp2);

        state[emptyY+1][emptyX] = tmp;
        state[emptyY][emptyX] = 6;
      }

      if (emptyX > 0) {
        int tmp = state[emptyY][emptyX-1];
        state[emptyY][emptyX] = to[tmp][0];
        state[emptyY][emptyX-1] = 6;

        int tmp2 = encode(state);
        if (!memo[tmp2]) que2.push(tmp2);

        state[emptyY][emptyX-1] = tmp;
        state[emptyY][emptyX] = 6;
      }

      if (emptyX+1 < 3) {
        int tmp = state[emptyY][emptyX+1];
        state[emptyY][emptyX] = to[tmp][0];
        state[emptyY][emptyX+1] = 6;

        int tmp2 = encode(state);
        if (!memo[tmp2]) que2.push(tmp2);

        state[emptyY][emptyX+1] = tmp;
        state[emptyY][emptyX] = 6;
      }
    }

    que = que2;
  }
}

int solve(int y, int x, int hash) {
  static int prevY = -1, prevX = -1;

  if (prevY != y || prevX != x) {
    memset(memo, 0, sizeof(memo));

    bfs(y, x);
  }

  int ans = INF, state[3][3];

  decode(hash, state);

  for (int i=0; i<(1<<9); i++) {

    for (int y=0; y<3; y++) {
      for (int x=0; x<3; x++) {
        if (state[y][x] < 6)
          state[y][x] = state[y][x] / 2 * 2 + (i >> (y * 3 + x)) % 2;
      }
    }

    int hash = encode(state);

    if (memo[hash] > 0)
      ans = min(ans, memo[hash] - 1);
  }

  prevY = y;
  prevX = x;

  return ans;
}

int main(){
  int x, y;
  int state[3][3];

  vector<pair<int, pair<int,int> > > query;

  for(int i=0; ;i++){
    scanf("%d%d", &x, &y);

    if (x == 0) break;
    x--; y--;

    int b = 2, r = 4;
    int rotate = 0;

    if ((y == 0 && x == 2) || (y == 1 && x == 2)) { rotate = 1; swap(b, r); }
    if ((y == 2 && x == 2) || (y == 2 && x == 1)) { rotate = 2; }
    if ((y == 2 && x == 0) || (y == 1 && x == 0)) { rotate = 3; swap(b, r); }

    for (int i=0; i<3; i++) {
      for (int j=0; j<3; j++) {
        char c;
        scanf(" %c", &c);

        if (c == 'W') state[i][j] = 0;
        if (c == 'B') state[i][j] = b;
        if (c == 'R') state[i][j] = r;
        if (c == 'E') state[i][j] = 6;
      }
    }

    for (int k=0; k<rotate; k++) {
      swap(state[0][0], state[0][2]);
      swap(state[0][2], state[2][2]);
      swap(state[2][2], state[2][0]);

      swap(state[0][1], state[1][2]);
      swap(state[1][2], state[2][1]);
      swap(state[2][1], state[1][0]);

      // (1, 2) => (0, 1)
      int tmp = x;
      x = y;
      y = 2 - tmp;
    }

    int hash = encode(state);

    debug(y); debug(x);
    debug(rotate);

    query.push_back({y * 3 + x, {hash, i}});
  }

  sort(query.begin(), query.end());

  vector<int> ans(query.size());

  for (int i=0; i<query.size(); i++) {
    int y = query[i].first / 3;
    int x = query[i].first % 3;
    int hash = query[i].second.first;
    int idx = query[i].second.second;

    ans[idx] = solve(y, x, hash);
  }

  for (int p : ans) {
    if (p == INF)
      puts("-1");
    else
      printf("%d\n", p);
  }

  return 0;
}



Measured memory_bytes: 67252