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 <queue>
#include <cassert>
#include <map>
#include <vector>
using namespace std;

char c[12][12];

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

int memo[12][12][12][1024];

struct NODE{
	int next;
	int x,y,prev;
	int cost;
	NODE(int x,int y,int prev,int next,int cost) : x(x) , y(y) , prev(prev) , next(next), cost(cost) {}
};

int dir(pair<int,int> a,pair<int,int> b){
	int ddx = a.first - b.first;
	int ddy = a.second - b.second;
	if( ddx == 0 ) return ddy < 0 ? 3 : 1;
	else if( ddy == 0 ) return ddx < 0 ? 0 : 2;
	else assert(0);
}

int atamaga[12][12];
int main(){
	for(int i = 0 ; i < 12 ; i++)
		for(int j = 0 ; j < 12 ; j++)
			c[i][j] = '#';

	int H,W,N;
	cin >> H >> W >> N;
	for(int i = 0 ; i < H ; i++){
		for(int j = 0 ; j < W ; j++){
			cin >> c[i+1][j+1];
		}
	}
	map<char,pair<int,int> >tmp;
	for(int i = 1 ; i <= H ; i++){
		for(int j = 1 ; j <= W ; j++){
			tmp[c[i][j]] = make_pair(j,i);
		}
	}
	string table = "edcbaS";
	int initdir = 0;
	for(int i = 0 ; i+1 < table.size() ; i++){
		initdir = initdir * 4 + dir(tmp[table[i+1]],tmp[table[i]]);
	}
	for(int i = 0 ; i < table.size() ; i++){
		int x = tmp[table[i]].first;
		int y = tmp[table[i]].second;
		c[y][x] = '.';
	}
	for(int i = 1 ; i <= N ; i++){
		int x = tmp['0'+i].first;
		int y = tmp['0'+i].second;
		atamaga[y][x] = i;
		c[y][x] = '.';
	}
	queue<NODE> Q;
	Q.push(NODE(tmp['S'].first,tmp['S'].second,initdir,1,0));
	while(Q.size()){
		NODE q = Q.front(); Q.pop();
		if( memo[q.y][q.x][q.next][q.prev]++ ) continue;
		if( q.next > N ){
			cout << q.cost << endl;
			return 0;
		}
		vector< pair<int,int> > dont;
		int tx = q.x , ty = q.y;
		int oo = q.prev;
		dont.push_back(make_pair(tx,ty));
		for(int i = 0 ; i < 4 ; i++){

			int dirdir = (oo+2) % 4;
			oo /= 4;
			tx += dx[dirdir];
			ty += dy[dirdir];
			dont.push_back(make_pair(tx,ty));
		}
		
		for(int i = 0 ; i < 4 ; i++){
			int tx = q.x + dx[i] , ty = q.y + dy[i];
			if( c[ty][tx] == '#' ) continue;
			int flag = 1;
			for(int j = 0 ; j < dont.size() ; j++)
				if( dont[j] == make_pair(tx,ty) ){
					flag = 0; break;
				}
			if(!flag) continue;
			Q.push(NODE(tx,ty,(q.prev*4+(i))%1024,q.next+(q.next==atamaga[ty][tx]),q.cost+1));
		}
		
	}
	cout << -1 << endl;
	
	
	
}