#include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>
 
using namespace std;
 
typedef long long ll;
 
class dice {
 public:
  int a, b, c;
  dice(int a = 0, int b = 1, int c = 2): a(a), b(b), c(c) {}
  bool operator<(const dice &t) const {
    return (a < t.a) || (a == t.a && b < t.b) || (a == t.a && b == t.b && c < t.c);
  }
};
 
vector <int> goal; int ans;
int ix, iy;
map<vector<dice>, int> memo;
int d[] = {-3, 1, 3, -1};
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
bool same(vector <int> &a, vector <int> &b) {
  for (int i = 0; i < a.size(); i++) {
    if (a[i] != b[i]) return false;
  }
  return true;
}
bool same(vector <int> &a, vector <dice> &b) {
  for (int i = 0; i < a.size(); i++) {
    if (a[i] != b[i].a) return false;
  }
  return true;
}
 
 
void rot(dice &p, int i) {
  if (i == 0 || i == 2) {
    swap(p.a, p.b);
  }else {
    swap(p.a, p.c);
  }
}
 
void view(vector <int> p) {
  for (int i = 0; i < p.size(); i++) {
    cout << p[i] << " ";
    if ((i+1)%3 == 0) cout << endl;
  }
  cout << endl;
}
void view(vector <dice> p) {
  for (int i = 0; i < p.size(); i++) {
    cout << p[i].a << " ";
    if ((i+1)%3 == 0) cout << endl;
  }
  cout << endl;
}
 
 
// int loopn = 0;
void rec(vector <dice> &p, int &x, int &y, int prex, int prey, int cnt) {
  // loopn++;
  if (ans <= cnt+abs(x-ix)+abs(y-iy)) return;
  if (cnt+abs(x-ix)+abs(y-iy) > 30) {
    return;
  }
  // vector <int> t;
  // for (int i = 0; i < 9; i++) t.push_back(p[i].a);
  // map<vector<dice>, int>::iterator it = memo.find(p);
  // if (it != memo.end()) {
  //   if ((*it).second <= cnt) {
  //     return;
  //   }
  // }
 
  //   // vector <int> pret = {0, 2, 0, 0, 0, 0, 0, 0, 0};
  //   // if (same(pret, p)) {
  //   //   if (cnt == 1) {
  //   //     cout << x << " " << y << endl;
  //   //     cout << (*it).second << " " << cnt << endl;
  //   //   }
  //   // }
 
  // // view(t);
  // memo[p] = cnt;
  if (same(goal, p)) {
    if (ans <= cnt) return;
    ans = cnt;
  }else {
    for (int i = 0; i < 4; i++) {
      int nx = x+dx[i];
      int ny = y+dy[i];
 
      if (nx < 0 || ny < 0 || nx > 2 || ny > 2) continue;
      if (nx == prex && ny == prey) continue;
      int xy = x+y*3, nxy = nx+ny*3;
      swap(p[xy], p[nxy]);
      rot(p[xy], (i+2)%4);
      // if (same(pret, np)) {
      //   if (cnt == 0) {
      //     cout << nx << " " << ny << endl;
      //     // view(np);
      //   }
      // }
       
 
      rec(p, nx, ny, x, y, cnt+1);
      rot(p[xy], (i+2)%4);
      swap(p[xy], p[nxy]);
    }
  }
}
 
int main() {
  while (true) {
    int x, y;
    cin >> x >> y;
    if (x == 0 && y == 0) break;
    x--; y--;
    vector <dice> init;
    for (int i = 0; i < 9; i++) init.push_back(dice());
    init[x+y*3] = dice(5, 0, 0);
    vector <int> empvec;
    map<vector<dice>, int> empmemo;
    memo = empmemo;
    goal = empvec;
    for (int i = 0; i < 9; i++) {
      char v;
      cin >> v;
      if (v == 'W') goal.push_back(0);
      if (v == 'R') goal.push_back(1);
      if (v == 'B') goal.push_back(2);
      if (v == 'E') goal.push_back(5);
      if (v == 'E') {
        ix = i%3;
        iy = i/3;
      }
    }
    ans = INT_MAX;
    rec(init, x, y, -1, -1, 0);
    // cout << memo.size() << endl;
    // cout << loopn << endl;
    if (ans == INT_MAX){
      cout << -1 << endl;
    }else {
      cout << ans << endl;
    }
  }
}

Measured memory_bytes: 1220