#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>

using namespace std;
typedef pair<int, int> pii;
typedef pair<int, string> pis;

class Dice {
	int face[6];
	void roll(int a, int b, int c, int d){
		int tmp = face[a];
		face[a] = face[b];
		face[b] = face[c];
		face[c] = face[d];
		face[d] = tmp;
	}
public:
	static const int TOP = 0, BACK = 1, LEFT = 2;
	static const int FRONT = 3, RIGHT = 4, BOTTOM = 5;
	Dice(){
		for(int i = 0; i < 6; ++i){ face[i] = i; }
	}
	int operator[](int index) const { return face[index]; }
	void roll_x_inc(){ roll(TOP, LEFT, BOTTOM, RIGHT); }
	void roll_x_dec(){ roll(TOP, RIGHT, BOTTOM, LEFT); }
	void roll_y_inc(){ roll(TOP, BACK, BOTTOM, FRONT); }
	void roll_y_dec(){ roll(TOP, FRONT, BOTTOM, BACK); }
	void roll_z_inc(){ roll(FRONT, LEFT, BACK, RIGHT); }
	void roll_z_dec(){ roll(FRONT, RIGHT, BACK, LEFT); }
	static vector<Dice> enumerate(){
		vector<Dice> ret;
		Dice d;
		for(int i = 0; i < 6; ++i){
			for(int j = 0; j < 4; ++j){
				ret.push_back(d);
				d.roll_z_inc();
			}
			if(i & 1){
				d.roll_y_inc();
			}else{
				d.roll_x_inc();
			}
		}
		return ret;
	}
};

const int dx[] = { 1, 0, -1, 0 };
const int dy[] = { 0, -1, 0, 1 };

bool between(int a, int b, int c){
	return a <= b && b < c;
}

int cell_dir(char c){
	switch(c){
		case '+': return 15;
		case '|': return 10;
		case '-': return 5;
		case '<': return 4;
		case '>': return 1;
		case '^': return 2;
		case 'v': return 8;
	}
	return 0;
}

int make_graph(
	const vector<string> &field,
	vector< vector<pii> > &conn, vector<int> &tops)
{
	int H = field.size(), W = field[0].size();
	conn.resize(H * W * 24);
	tops.resize(24);
	vector< vector<int> > roll_table(6, vector<int>(6));
	vector<Dice> dices = Dice::enumerate();
	int start_face = 0;
	for(int i = 0; i < dices.size(); ++i){
		if(dices[i][Dice::TOP] == 0 && dices[i][Dice::BACK] == 1){
			start_face = i;
		}
		roll_table[dices[i][Dice::TOP]][dices[i][Dice::FRONT]] = i;
		tops[i] = dices[i][Dice::TOP];
	}
	for(int i = 0; i < H; ++i){
		for(int j = 0; j < W; ++j){
			int dir = cell_dir(field[i][j]);
			for(int k = 0; k < 4; ++k){
				if(!(dir & (1 << k))){ continue; }
				int nr = i + dy[k], nc = j + dx[k];
				if(!between(0, nr, H) || !between(0, nc, W)){ continue; }
				for(int l = 0; l < 24; ++l){
					Dice d = dices[l];
					switch(k){
						case 0: d.roll_x_inc(); break;
						case 1: d.roll_y_dec(); break;
						case 2: d.roll_x_dec(); break;
						case 3: d.roll_y_inc(); break;
					}
					int nd = roll_table[d[Dice::TOP]][d[Dice::FRONT]];
					int cur = ((i * W) + j) * 24 + l;
					int next = ((nr * W) + nc) * 24 + nd;
					conn[next].push_back(pii(cur, dices[l][Dice::TOP]));
				}
			}
		}
	}
	return start_face;
}

bool is_reachable(const vector< vector<pii> > &conn, int start, int goal){
	int n = conn.size();
	vector<bool> passed(n);
	queue<int> q;
	q.push(start);
	passed[start] = true;
	while(!q.empty()){
		int p = q.front();
		if(p == goal){ return true; }
		q.pop();
		for(int i = 0; i < conn[p].size(); ++i){
			if(passed[conn[p][i].first]){ continue; }
			passed[conn[p][i].first] = true;
			q.push(conn[p][i].first);
		}
	}
	return false;
}

int count_reachable(const vector< vector<pii> > &conn, const vector<int> &goals){
	int n = conn.size();
	vector<bool> passed(n);
	queue<int> q;
	for(int i = 0; i < goals.size(); ++i){
		passed[goals[i]] = true;
		q.push(goals[i]);
	}
	while(!q.empty()){
		int p = q.front();
		q.pop();
		for(int i = 0; i < conn[p].size(); ++i){
			if(passed[conn[p][i].first]){ continue; }
			passed[conn[p][i].first] = true;
			q.push(conn[p][i].first);
		}
	}
	int answer = 0;
	for(int i = 0; i < n; ++i){
		if(passed[i]){ ++answer; }
	}
	return answer;
}

int main(){
	int H, W;
	cin >> H >> W;
	vector<string> field(H);
	for(int i = 0; i < H; ++i){ cin >> field[i]; }
	vector<string> face(6);
	int maxlen = 0;
	for(int i = 0; i < 6; ++i){
		cin >> face[i];
		maxlen = max(maxlen, (int)face[i].size());
	}
	int Rs, Cs, Rd, Cd;
	cin >> Rs >> Cs >> Rd >> Cd;
	--Rs; --Cs; --Rd; --Cd;
	vector< vector<pii> > conn;
	vector<int> tops;
	int start = make_graph(field, conn, tops);
	start += ((Rs * W) + Cs) * 24;
	vector<int> goals;
	for(int i = 0; i < 24; ++i){
		int node = ((Rd * W) + Cd) * 24 + i;
		if(is_reachable(conn, node, start)){ goals.push_back(node); }
	}
	if(goals.size() == 0){
		cout << "no" << endl;
		return 0;
	}
	int n = conn.size();
	vector<string> mincost(conn.size());
	vector<bool> is_goal(conn.size());
	for(int i = 0; i < goals.size(); ++i){
		mincost[goals[i]] = face[tops[goals[i] % 24]];
		is_goal[goals[i]] = true;
	}
	int internal_n = count_reachable(conn, goals);
	for(int t = 0; t < internal_n * (maxlen + 1); ++t){
		vector<string> mc(mincost);
		bool modified = false;
		for(int i = 0; i < n; ++i){
			if(mincost[i] == ""){ continue; }
			for(int j = 0; j < conn[i].size(); ++j){
				int from = i, to = conn[i][j].first;
				int top = conn[i][j].second;
				if(mincost[to] == "" || mc[to] > face[top] + mincost[from]){
					mc[to] = face[top] + mincost[from];
					modified = true;
					if(to == start && t >= internal_n){
						cout << "infinite" << endl;
						return 0;
					}
				}
			}
		}
		mincost.swap(mc);
		if(!modified){ break; }
	}
	cout << mincost[start] << endl;
	return 0;
}
### Prompt

Please provide a Cpp coded solution to the problem described below:

There is a cube on a rectangle map with H-rows and W-columns grid. Two special squares a start and a goal are marked on the map. Initially, the cube is on the start square. Let's repeat to roll it and take it to the goal square. Rolling the cube means to select one of four edges which touch the map, and push the cube down without detaching the edge from the map. That is, there are four directions you can move the cube toward.

Directions where we can roll the cube are limited depending on each square. An instruction is written in each square and represented by a single character as follows:

'+'
all
'|'
only vertical
'-'
only horizontal
'<'
only to left
'>'
only to right
'^'
only to up
'v'
only to down
'.'
none

Regardless of instructions, it is not allowed to roll the cube to outside of the map.

On each face of the cube, a string is written. Let's output the string which concatenates strings written on the top face seen during the rollings from the start square to the goal square. Since there may be multiple paths that take the cube to the goal square, choose the minimal string in ascending lexicographic order.

Please note that there are cases where no path exists from the start to the goal, or the cases you can make the lexicographically minimal string infinitely longer.



Input

A data set has following format:

> H W
>  C11 ... C1W
>  ...
>  CH1 ... CHW
>  T1
>  ...
>  T6
>  RS CS
>  RD CD
>

The first line of the input contains two integers H (1 ≤ H ≤ 12) and W (1 ≤ W ≤ 12), which indicate the number of rows and columns of the map respectively. The following W lines describe the map. The j-th character of the i-th line indicates the instruction of the square, which is placed on i-th row (from the top) and j-th column (from the left).

Then the following 6 lines describe the strings on each face of the cube. All of these strings are not empty and shorter than 12 characters (inclusive). In addition, they only consist of uppercase alphabets or digits. The faces where the strings are written are given as figure 1. Initially, the cube is placed on the start square in a direction as the face No. 1 is facing top and the upper direction of face No. 1 faces toward the top row of the map.

<image>

Figure 1. a net of a cube

The last two lines contain two integers each that indicate the row number and column number of the start square and the goal square in this order. You can assume that the start square and the goal square are always different.

Output

Print the lexicographically minimal string in a line. If there is no path, print "no" in a line. If you can make the lexicographically minimal string infinitely longer, print "infinite" in a line.

Examples

Input

1 3
+++
6
5
4
3
2
1
1 3
1 1


Output

621


Input

1 3
+++
1
2
3
4
5
6
1 3
1 1


Output

infinite


Input

1 3
...
1
2
3
4
5
6
1 3
1 1


Output

no


Input

3 3
->|
..v
.^<
JAG
2012
SUMMER
HOGE
HOGE
CAMP
1 1
2 2


Output

JAGSUMMERCAMP2012JAGSUMMER2012

### Response

```cpp
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>

using namespace std;
typedef pair<int, int> pii;
typedef pair<int, string> pis;

class Dice {
	int face[6];
	void roll(int a, int b, int c, int d){
		int tmp = face[a];
		face[a] = face[b];
		face[b] = face[c];
		face[c] = face[d];
		face[d] = tmp;
	}
public:
	static const int TOP = 0, BACK = 1, LEFT = 2;
	static const int FRONT = 3, RIGHT = 4, BOTTOM = 5;
	Dice(){
		for(int i = 0; i < 6; ++i){ face[i] = i; }
	}
	int operator[](int index) const { return face[index]; }
	void roll_x_inc(){ roll(TOP, LEFT, BOTTOM, RIGHT); }
	void roll_x_dec(){ roll(TOP, RIGHT, BOTTOM, LEFT); }
	void roll_y_inc(){ roll(TOP, BACK, BOTTOM, FRONT); }
	void roll_y_dec(){ roll(TOP, FRONT, BOTTOM, BACK); }
	void roll_z_inc(){ roll(FRONT, LEFT, BACK, RIGHT); }
	void roll_z_dec(){ roll(FRONT, RIGHT, BACK, LEFT); }
	static vector<Dice> enumerate(){
		vector<Dice> ret;
		Dice d;
		for(int i = 0; i < 6; ++i){
			for(int j = 0; j < 4; ++j){
				ret.push_back(d);
				d.roll_z_inc();
			}
			if(i & 1){
				d.roll_y_inc();
			}else{
				d.roll_x_inc();
			}
		}
		return ret;
	}
};

const int dx[] = { 1, 0, -1, 0 };
const int dy[] = { 0, -1, 0, 1 };

bool between(int a, int b, int c){
	return a <= b && b < c;
}

int cell_dir(char c){
	switch(c){
		case '+': return 15;
		case '|': return 10;
		case '-': return 5;
		case '<': return 4;
		case '>': return 1;
		case '^': return 2;
		case 'v': return 8;
	}
	return 0;
}

int make_graph(
	const vector<string> &field,
	vector< vector<pii> > &conn, vector<int> &tops)
{
	int H = field.size(), W = field[0].size();
	conn.resize(H * W * 24);
	tops.resize(24);
	vector< vector<int> > roll_table(6, vector<int>(6));
	vector<Dice> dices = Dice::enumerate();
	int start_face = 0;
	for(int i = 0; i < dices.size(); ++i){
		if(dices[i][Dice::TOP] == 0 && dices[i][Dice::BACK] == 1){
			start_face = i;
		}
		roll_table[dices[i][Dice::TOP]][dices[i][Dice::FRONT]] = i;
		tops[i] = dices[i][Dice::TOP];
	}
	for(int i = 0; i < H; ++i){
		for(int j = 0; j < W; ++j){
			int dir = cell_dir(field[i][j]);
			for(int k = 0; k < 4; ++k){
				if(!(dir & (1 << k))){ continue; }
				int nr = i + dy[k], nc = j + dx[k];
				if(!between(0, nr, H) || !between(0, nc, W)){ continue; }
				for(int l = 0; l < 24; ++l){
					Dice d = dices[l];
					switch(k){
						case 0: d.roll_x_inc(); break;
						case 1: d.roll_y_dec(); break;
						case 2: d.roll_x_dec(); break;
						case 3: d.roll_y_inc(); break;
					}
					int nd = roll_table[d[Dice::TOP]][d[Dice::FRONT]];
					int cur = ((i * W) + j) * 24 + l;
					int next = ((nr * W) + nc) * 24 + nd;
					conn[next].push_back(pii(cur, dices[l][Dice::TOP]));
				}
			}
		}
	}
	return start_face;
}

bool is_reachable(const vector< vector<pii> > &conn, int start, int goal){
	int n = conn.size();
	vector<bool> passed(n);
	queue<int> q;
	q.push(start);
	passed[start] = true;
	while(!q.empty()){
		int p = q.front();
		if(p == goal){ return true; }
		q.pop();
		for(int i = 0; i < conn[p].size(); ++i){
			if(passed[conn[p][i].first]){ continue; }
			passed[conn[p][i].first] = true;
			q.push(conn[p][i].first);
		}
	}
	return false;
}

int count_reachable(const vector< vector<pii> > &conn, const vector<int> &goals){
	int n = conn.size();
	vector<bool> passed(n);
	queue<int> q;
	for(int i = 0; i < goals.size(); ++i){
		passed[goals[i]] = true;
		q.push(goals[i]);
	}
	while(!q.empty()){
		int p = q.front();
		q.pop();
		for(int i = 0; i < conn[p].size(); ++i){
			if(passed[conn[p][i].first]){ continue; }
			passed[conn[p][i].first] = true;
			q.push(conn[p][i].first);
		}
	}
	int answer = 0;
	for(int i = 0; i < n; ++i){
		if(passed[i]){ ++answer; }
	}
	return answer;
}

int main(){
	int H, W;
	cin >> H >> W;
	vector<string> field(H);
	for(int i = 0; i < H; ++i){ cin >> field[i]; }
	vector<string> face(6);
	int maxlen = 0;
	for(int i = 0; i < 6; ++i){
		cin >> face[i];
		maxlen = max(maxlen, (int)face[i].size());
	}
	int Rs, Cs, Rd, Cd;
	cin >> Rs >> Cs >> Rd >> Cd;
	--Rs; --Cs; --Rd; --Cd;
	vector< vector<pii> > conn;
	vector<int> tops;
	int start = make_graph(field, conn, tops);
	start += ((Rs * W) + Cs) * 24;
	vector<int> goals;
	for(int i = 0; i < 24; ++i){
		int node = ((Rd * W) + Cd) * 24 + i;
		if(is_reachable(conn, node, start)){ goals.push_back(node); }
	}
	if(goals.size() == 0){
		cout << "no" << endl;
		return 0;
	}
	int n = conn.size();
	vector<string> mincost(conn.size());
	vector<bool> is_goal(conn.size());
	for(int i = 0; i < goals.size(); ++i){
		mincost[goals[i]] = face[tops[goals[i] % 24]];
		is_goal[goals[i]] = true;
	}
	int internal_n = count_reachable(conn, goals);
	for(int t = 0; t < internal_n * (maxlen + 1); ++t){
		vector<string> mc(mincost);
		bool modified = false;
		for(int i = 0; i < n; ++i){
			if(mincost[i] == ""){ continue; }
			for(int j = 0; j < conn[i].size(); ++j){
				int from = i, to = conn[i][j].first;
				int top = conn[i][j].second;
				if(mincost[to] == "" || mc[to] > face[top] + mincost[from]){
					mc[to] = face[top] + mincost[from];
					modified = true;
					if(to == start && t >= internal_n){
						cout << "infinite" << endl;
						return 0;
					}
				}
			}
		}
		mincost.swap(mc);
		if(!modified){ break; }
	}
	cout << mincost[start] << endl;
	return 0;
}
```