Problem: G, a college student living in a certain sky city, has a hornworm, Imotaro. He disciplined Imotaro to eat all the food in order with the shortest number of steps. You, his friend, decided to write a program because he asked me to find out if Imotaro was really disciplined.



Input


H W N
area


Input is given in H + 1 lines. The first line contains three integers H, W, N (2 ≤ H ≤ 10, 2 ≤ W ≤ 10, 1 ≤ N ≤ 9). H is the height of the area, W is the width, and N is the number of foods (at this time, 6 + N ≤ H × W always holds).

In each line from the 2nd line to H + 1st line, ´S´, ´1´, 2´,… ´9´, ´a´, ´b´, ´c´, ´d´, ´e´, A W character string consisting of ´ # ´ and ´.´ is written, and each represents the state of each section of the area. In addition, ´S´, ´a´, ´b´, ´c´, ´d´, ´e´ represent the initial state of the hornworm. There is no food or obstacles so that it is covered with the hornworm in the initial state. In addition, the initial position of the hornworm and the position of the food are guaranteed to be entered correctly.

Output

Output the minimum number of steps when the hornworm eats food in order, or -1 if that is not possible.

Examples

Input

5 8 3
#.......
#.####2#
#.#.3..#
#.######
.1Sabcde


Output

14


Input

5 8 3
.......
.####2#
.#.3..#
.######
.1Sabcde


Output

14


Input

2 6 2
.1.baS
.2.cde


Output

7


Input

2 6 2
.1#baS
.2.cde


Output

-1

Solution (in CPP):

Solution:
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
struct State {
	int c, x, y, s;
	explicit State(int c = 0, int x = 0, int y = 0, int s = 0) :
		c(c), x(x), y(y), s(s)
	{ }
};
const int INF = 1000000000;
const int dx[] = { 1, 0, -1, 0 };
const int dy[] = { 0, -1, 0, 1 };

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

int dist[10][10][10][1 << 10];

int main(){
	ios_base::sync_with_stdio(false);
	int h, w, n;
	cin >> h >> w >> n;
	vector<string> field(h);
	for(int i = 0; i < h; ++i){
		cin >> field[i];
		for(int j = 0; j < w; ++j){
			if(islower(field[i][j])){ ++field[i][j]; }
			if(field[i][j] == 'S'){ field[i][j] = 'a'; }
			if(isdigit(field[i][j])){ --field[i][j]; }
		}
	}
	int init_s = 0, init_x = 0, init_y = 0;
	for(int i = 0; i < h; ++i){
		for(int j = 0; j < w; ++j){
			if(field[i][j] == 'a'){ init_x = j; init_y = i; }
			if('b' <= field[i][j] && field[i][j] <= 'f'){
				for(int d = 0; d < 4; ++d){
					const int y = i + dy[d], x = j + dx[d];
					if(!between(0, y, h) || !between(0, x, w)){ continue; }
					if(field[y][x] == field[i][j] - 1){
						init_s |= (d << ((field[i][j] - 'b') * 2));
					}
				}
			}
		}
	}
	vector<int> valid_state(1 << 10);
	for(int i = 0; i < (1 << 10); ++i){
		int bitmap[11][11] = { { 0 } };
		int x = 5, y = 5, valid = 1;
		bitmap[y][x] = 1;
		for(int j = 4; j >= 0; --j){
			const int d = (i >> (j * 2)) & 3;
			x += dx[d];
			y += dy[d];
			if(bitmap[y][x]){ valid = 0; }
			bitmap[y][x] = 1;
		}
		valid_state[i] = valid;
	}
	for(int i = 0; i <= n; ++i){
		for(int y = 0; y < h; ++y){
			for(int x = 0; x < w; ++x){
				for(int s = 0; s < (1 << 10); ++s){ dist[i][y][x][s] = INF; }
			}
		}
	}
	queue<State> q;
	dist[0][init_y][init_x][init_s] = 0;
	q.push(State(0, init_x, init_y, init_s));
	int answer = INF;
	while(!q.empty()){
		const State p = q.front();
		q.pop();
		const int d = dist[p.c][p.y][p.x][p.s];
		if(p.c == n){
			answer = d;
			break;
		}
		for(int dir = 0; dir < 4; ++dir){
			const int nx = p.x + dx[dir], ny = p.y + dy[dir];
			if(!between(0, nx, w) || !between(0, ny, h)){ continue; }
			if(field[ny][nx] == '#'){ continue; }
			const int ns = ((p.s << 2) | dir) & ((1 << 10) - 1);
			const int nc = p.c + (field[ny][nx] == '0' + p.c ? 1 : 0);
			if(valid_state[ns] && d + 1 < dist[nc][ny][nx][ns]){
				dist[nc][ny][nx][ns] = d + 1;
				q.push(State(nc, nx, ny, ns));
			}
		}
	}
	if(answer >= INF){ answer = -1; }
	cout << answer << endl;
	return 0;
}