Problem: On a small planet named Bandai, a landing party of the starship Tadamigawa discovered colorful cubes traveling on flat areas of the planet surface, which the landing party named beds. A cube appears at a certain position on a bed, travels on the bed for a while, and then disappears. After a longtime observation, a science officer Lt. Alyssa Ogawa of Tadamigawa found the rule how a cube travels on a bed.

A bed is a rectangular area tiled with squares of the same size.

* One of the squares is colored red,
* one colored green,
* one colored blue,
* one colored cyan,
* one colored magenta,
* one colored yellow,
* one or more colored white, and
* all others, if any, colored black.



Initially, a cube appears on one of the white squares. The cube’s faces are colored as follows.


top  red
bottom  cyan
north  green
south  magenta
east  blue
west  yellow


The cube can roll around a side of the current square at a step and thus rolls on to an adjacent square. When the cube rolls on to a chromatically colored (red, green, blue, cyan, magenta or yellow) square, the top face of the cube after the roll should be colored the same. When the cube rolls on to a white square, there is no such restriction. The cube should never roll on to a black square.

Throughout the travel, the cube can visit each of the chromatically colored squares only once, and any of the white squares arbitrarily many times. As already mentioned, the cube can never visit any of the black squares. On visit to the final chromatically colored square, the cube disappears. Somehow the order of visits to the chromatically colored squares is known to us before the travel starts.

Your mission is to find the least number of steps for the cube to visit all the chromatically colored squares in the given order.



Input

The input is a sequence of datasets. A dataset is formatted as follows:


w d

c11 . . . cw1
.        .
.        .
.        .
c1d . . . cwd
v1v2v3v4v5v6


The first line is a pair of positive integers w and d separated by a space. The next d lines are w-character-long strings c11 . . . cw1 , . . . , c1d . . . cwd with no spaces. Each character cij is one of the letters r, g, b, c, m, y, w and k, which stands for red, green, blue, cyan, magenta, yellow, white and black respectively, or a sign #. Each of r, g, b, c, m, y and # occurs once and only once in a dataset. The last line is a six-character-long string v1v2v3v4v5v6 which is a permutation of “rgbcmy”.

The integers w and d denote the width (the length from the east end to the west end) and the depth (the length from the north end to the south end) of a bed. The unit is the length of a side of a square. You can assume that neither w nor d is greater than 30.

Each character cij shows the color of a square in the bed. The characters c11 , cw1 , c1d and cwd correspond to the north-west corner, the north-east corner, the south-west corner and the south- east corner of the bed respectively. If cij is a letter, it indicates the color of the corresponding square. If cij is a #, the corresponding square is colored white and is the initial position of the cube.

The string v1v2v3v4v5v6 shows the order of colors of squares to visit. The cube should visit the squares colored v1, v2 , v3 , v4 , v5 and v6 in this order.

The end of the input is indicated by a line containing two zeros separated by a space.

Output

For each input dataset, output the least number of steps if there is a solution, or "unreachable" if there is no solution. In either case, print it in one line for each input dataset.

Example

Input

10 5
kkkkkwwwww
w#wwwrwwww
wwwwbgwwww
kwwmcwwwkk
kkwywwwkkk
rgbcmy
10 5
kkkkkkkkkk
k#kkkkkkkk
kwkkkkkwwk
kcmyrgbwwk
kwwwwwwwwk
cmyrgb
10 5
kkkkkkkkkk
k#kkkkkkkk
kwkkkkkwkk
kcmyrgbwwk
kwwwwwwwwk
cmyrgb
0 0


Output

9
49
unreachable

Solution (in CPP):

Solution:
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum Color{
	red,
	cyan,
	green,
	magenta,
	blue,
	yellow,
	white,
	black,
};

char order[25] = "NNNNWNNNWNNNENNNENNNWNNN";

class Cube{
public:
	Cube(){
		init();
	};

	void roll(char dst){
		for(int i = 0; i < 6; i++) work[i] = colors[i];
		switch(dst){
		case 'E':
			setColor(work[3],work[1],work[0],work[5],work[4],work[2]);
			break;
		case 'S':
			setColor(work[1],work[5],work[2],work[3],work[0],work[4]);
			break;
		case 'N':
			setColor(work[4],work[0],work[2],work[3],work[5],work[1]);
			break;
		case 'W':
			setColor(work[2],work[1],work[5],work[0],work[4],work[3]);
			break;
		}
	};

	Color getTop(){
		return colors[0];
	}

	int getPos(){

		Color tmp_colors[6] = {red,green,blue,yellow,magenta,cyan};
		Color tmp_work[6];

		int ret = -1;

		for(int loop = 0; loop < 24; loop++){
			for(int i = 0; i < 6; i++) tmp_work[i] = tmp_colors[i];

			switch(order[loop]){
			case 'E':
				tmp_colors[0] = tmp_work[3];
				tmp_colors[1] = tmp_work[1];
				tmp_colors[2] = tmp_work[0];
				tmp_colors[3] = tmp_work[5];
				tmp_colors[4] = tmp_work[4];
				tmp_colors[5] = tmp_work[2];
				break;
			case 'S':
				tmp_colors[0] = tmp_work[1];
				tmp_colors[1] = tmp_work[5];
				tmp_colors[2] = tmp_work[2];
				tmp_colors[3] = tmp_work[3];
				tmp_colors[4] = tmp_work[0];
				tmp_colors[5] = tmp_work[4];
				break;
			case 'N':
				tmp_colors[0] = tmp_work[4];
				tmp_colors[1] = tmp_work[0];
				tmp_colors[2] = tmp_work[2];
				tmp_colors[3] = tmp_work[3];
				tmp_colors[4] = tmp_work[5];
				tmp_colors[5] = tmp_work[1];
				break;
			case 'W':
				tmp_colors[0] = tmp_work[2];
				tmp_colors[1] = tmp_work[1];
				tmp_colors[2] = tmp_work[5];
				tmp_colors[3] = tmp_work[0];
				tmp_colors[4] = tmp_work[4];
				tmp_colors[5] = tmp_work[3];
				break;
			}

			for(int i = 0; i < 6; i++){
				if(tmp_colors[i] != colors[i])break;
				if(i == 5){
					ret = loop;
					break;
				}
			}
			if(ret != -1)break;
		}
		return ret;
	}

	void setColor(Color n0,Color n1,Color n2,Color n3,Color n4,Color n5){
		colors[0] = n0;
		colors[1] = n1;
		colors[2] = n2;
		colors[3] = n3;
		colors[4] = n4;
		colors[5] = n5;
	}

	void init(){
		setColor(red,green,blue,yellow,magenta,cyan);
	}

	void set(Cube arg){
		for(int i = 0; i < 6; i++){
			colors[i] = arg.colors[i];
		}
	}

	Color colors[6];
	Color work[6];
};

struct Info{
	int row,col;
};

struct Data{
	void set(int arg_row,int arg_col,int arg_state,Color arg_last_visited,int arg_cost){
		loc.row = arg_row;
		loc.col = arg_col;
		state = arg_state;
		last_visited = arg_last_visited;
		cost = arg_cost;
	}
	Cube cube;
	Info loc;
	int state,cost;
	Color last_visited;
};

int H,W,diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0},POW[6];
char map[30][31],dir[4] = {'N','W','E','S'};
int dp[30][30][64][24]; //dp[row][col][state][cube-pos]
Color visit_order[6];
Info info[6];

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
	else{
		return false;
	}
}

void func(){

	for(int a = 0; a < H; a++){
		for(int b = 0;b < W;b++){
			for(int c = 0; c < 64; c++){
				for(int d = 0; d < 24; d++)dp[a][b][c][d] = BIG_NUM;
			}
		}
	}

	int start_row,start_col;

	for(int i = 0; i < H; i++){
		scanf("%s",map[i]);
		for(int k = 0; k < W; k++){
			switch(map[i][k]){
			case '#':
				start_row = i;
				start_col = k;
				map[i][k] = 'w';
				break;
			case 'r':
				info[red].row = i;
				info[red].col = k;
				break;
			case 'g':
				info[green].row = i;
				info[green].col = k;
				break;
			case 'b':
				info[blue].row = i;
				info[blue].col = k;
				break;
			case 'c':
				info[cyan].row = i;
				info[cyan].col = k;
				break;
			case 'm':
				info[magenta].row = i;
				info[magenta].col = k;
				break;
			case 'y':
				info[yellow].row = i;
				info[yellow].col = k;
				break;
			}
		}
	}

	char buf[7];
	scanf("%s",buf);

	for(int i = 0; i < 6; i++){

		switch(buf[i]){
		case 'r':
			visit_order[i] = red;
			break;
		case 'g':
			visit_order[i] = green;
			break;
		case 'b':
			visit_order[i] = blue;
			break;
		case 'c':
			visit_order[i] = cyan;
			break;
		case 'm':
			visit_order[i] = magenta;
			break;
		case 'y':
			visit_order[i] = yellow;
			break;
		}
	}

	Color last_color = visit_order[5];

	Color pre_color[6];
	pre_color[visit_order[0]] = white;

	for(int i = 1; i <= 5; i++){
		pre_color[visit_order[i]] = visit_order[i-1];
	}

	Data first;
	first.set(start_row,start_col,0,white,0);

	dp[start_row][start_col][0][first.cube.getPos()] = 0;

	queue<Data> Q;
	Q.push(first);

	int next_row,next_col,next_state,tmp;

	while(!Q.empty()){

		if(Q.front().loc.row == info[last_color].row && Q.front().loc.col == info[last_color].col){
			printf("%d\n",Q.front().cost);
			return;
		}else if(Q.front().cost > dp[Q.front().loc.row][Q.front().loc.col][Q.front().state][Q.front().cube.getPos()]){
			Q.pop();
		}else{

			for(int i = 0; i < 4; i++){
				next_row = Q.front().loc.row + diff_row[i];
				next_col = Q.front().loc.col + diff_col[i];

				if(rangeCheck(next_row,next_col) == false || map[next_row][next_col] == 'k')continue;

				Data next_data;
				next_data.cube.set(Q.front().cube);

				next_data.cube.roll(dir[i]);

				tmp = next_data.cube.getPos();

				switch(map[next_row][next_col]){
				case 'w':
					if(dp[next_row][next_col][Q.front().state][tmp] > Q.front().cost+1){
						dp[next_row][next_col][Q.front().state][tmp] = Q.front().cost+1;
						next_data.set(next_row,next_col,Q.front().state,Q.front().last_visited,Q.front().cost+1);
						Q.push(next_data);
					}
					break;
				case 'r':
					if(Q.front().last_visited == pre_color[red] && next_data.cube.getTop() == red){
						next_state = Q.front().state + POW[red];

						if(dp[next_row][next_col][next_state][tmp] > Q.front().cost+1){
							dp[next_row][next_col][next_state][tmp] = Q.front().cost+1;
							next_data.set(next_row,next_col,next_state,red,Q.front().cost+1);
							Q.push(next_data);
						}
					}
					break;
				case 'g':
					if(Q.front().last_visited == pre_color[green] && next_data.cube.getTop() == green){
						next_state = Q.front().state + POW[green];

						if(dp[next_row][next_col][next_state][tmp] > Q.front().cost+1){
							dp[next_row][next_col][next_state][tmp] = Q.front().cost+1;
							next_data.set(next_row,next_col,next_state,green,Q.front().cost+1);
							Q.push(next_data);
						}
					}
					break;
				case 'b':
					if(Q.front().last_visited == pre_color[blue] && next_data.cube.getTop() == blue){
						next_state = Q.front().state + POW[blue];

						if(dp[next_row][next_col][next_state][tmp] > Q.front().cost+1){
							dp[next_row][next_col][next_state][tmp] = Q.front().cost+1;
							next_data.set(next_row,next_col,next_state,blue,Q.front().cost+1);
							Q.push(next_data);
						}
					}
					break;
				case 'c':
					if(Q.front().last_visited == pre_color[cyan] && next_data.cube.getTop() == cyan){
						next_state = Q.front().state + POW[cyan];

						if(dp[next_row][next_col][next_state][tmp] > Q.front().cost+1){
							dp[next_row][next_col][next_state][tmp] = Q.front().cost+1;
							next_data.set(next_row,next_col,next_state,cyan,Q.front().cost+1);
							Q.push(next_data);
						}
					}
					break;
				case 'm':
					if(Q.front().last_visited == pre_color[magenta] && next_data.cube.getTop() == magenta){
						next_state = Q.front().state + POW[magenta];

						if(dp[next_row][next_col][next_state][tmp] > Q.front().cost+1){
							dp[next_row][next_col][next_state][tmp] = Q.front().cost+1;
							next_data.set(next_row,next_col,next_state,magenta,Q.front().cost+1);
							Q.push(next_data);
						}
					}
					break;
				case 'y':
					if(Q.front().last_visited == pre_color[yellow] && next_data.cube.getTop() == yellow){
						next_state = Q.front().state + POW[yellow];

						if(dp[next_row][next_col][next_state][tmp] > Q.front().cost+1){
							dp[next_row][next_col][next_state][tmp] = Q.front().cost+1;
							next_data.set(next_row,next_col,next_state,yellow,Q.front().cost+1);
							Q.push(next_data);
						}
					}
					break;
				}
			}
			Q.pop();
		}
	}

	printf("unreachable\n");
}

int main(){

	for(int i = 0; i <= 5; i++)POW[i] = pow(2,i);

	while(true){
		scanf("%d %d",&W,&H);
		if(W == 0 && H == 0)break;

		func();
	}

	return 0;
}