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<bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

#define F first
#define S second

int h, w, a;
string in[10];

inline bool ok(const vi &x, const vi& y){
	rep(i, 6) rep(j, i) if(x[i] == x[j] && y[i] == y[j]) return 0;
	return 1;
}

int main(){
	
	cin >> h >> w >> a;
	
	vi gy(a), gx(a);
	vi py(6), px(6);
	
	rep(i, h){
		cin >> in[i];
		
		rep(j, w){
			char c = in[i][j];
			
			if(isalpha(c)){
				
				if(c == 'S') py[0] = i, px[0] = j;
				else py[c - 'a' + 1] = i, px[c - 'a' + 1] = j;
			}
			if(isdigit(c)) gy[c - '1'] = i, gx[c - '1'] = j;
		}
	}
	
	set<pair<int, pair<vi, vi> > > s; //esa, pos;
	queue<pair<pi, pair<vi, vi> > > q; //cost, esa, pos
	
	s.insert(mp(0, mp(py, px)));
	q.push(mp(mp(0, 0), mp(py, px)));
	
	while(!q.empty()){
		
		int cost = q.front().F.F;
		int esa = q.front().F.S;
		vi ys = q.front().S.F;
		vi xs = q.front().S.S;
		
		q.pop();
		cost++;
		
		rep(d, 4){
			const int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};
			int hy = ys[0] + dy[d], hx = xs[0] + dx[d];
			
			if(hy < 0 || hy >= h || hx < 0 || hx >= w) continue;
			if(in[hy][hx] == '#') continue;
			
			vi ny, nx;
			ny.pb(hy); nx.pb(hx);
			rep(i, 5) ny.pb(ys[i]), nx.pb(xs[i]);
			
			if(!ok(ny, nx)) continue;
			
			int nesa = esa;
			if(in[hy][hx] == '1' + esa){
				if(++nesa == a){
					cout << cost << endl;
					return 0;
				}
			}
			
			if(s.count(mp(nesa, mp(ny, nx)))) continue;
			s.insert(mp(nesa, mp(ny, nx)));
			q.push(mp(mp(cost, nesa), mp(ny, nx)));
		}
	}
	cout << -1 << endl;
	
	return 0;
}