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:
/*
 * h.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_H = 10;
const int MAX_W = 10;
const int MAX_N = 9;
const int M = 5;
const int DBITS = 1 << (M * 2);

const int INF = 1 << 30;

const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1};

/* typedef */

struct Stat {
  int d, k, x, y, dbits;
};

/* global variables */

string flds[MAX_H];
int xs[M], ys[M];
int dists[MAX_N][MAX_H][MAX_W][DBITS];

/* subroutines */

bool move(Stat &s, int h, int w, int di) {
  int x0 = s.x + dxs[di], y0 = s.y + dys[di];
  if (x0 < 0 || x0 >= w || y0 < 0 || y0 >= h || flds[y0][x0] == '#')
    return false;
  for (int i = 0, x = s.x, y = s.y; i < M - 1; i++) {
    int dii = ((s.dbits >> (i * 2)) & 3);
    x += dxs[dii], y += dys[dii];
    if (x0 == x && y0 == y) return false;
  }

  s.dbits = ((s.dbits << 2) & (DBITS - 1)) | ((di + 2) & 3);
  s.x = x0, s.y = y0;
  s.d++;
  return true;
}

/* main */

int main() {
  int h, w, n;
  cin >> h >> w >> n;

  Stat st;
  st.d = st.k = st.dbits = 0;

  for (int y = 0; y < h; y++) {
    cin >> flds[y];
    for (int x = 0; x < w; x++) {
      char ch = flds[y][x];
      if (ch == 'S') st.x = x, st.y = y;
      else if (ch >= 'a' && ch <= 'e') {
	int id = ch - 'a';
	xs[id] = x, ys[id] = y;
      }
    }
  }

  for (int i = 0, x = st.x, y = st.y; i < M; i++) {
    int dx = xs[i] - x, dy = ys[i] - y;
    for (int di = 0; di < 4; di++)
      if (dx == dxs[di] && dy == dys[di]) {
	st.dbits |= (di << (i * 2));
	break;
      }
    x = xs[i], y = ys[i];
  }
  
  for (int i = 0; i < n; i++)
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++)
	for (int bits = 0; bits < DBITS; bits++)
	  dists[i][y][x][bits] = INF;
  dists[0][st.y][st.x][st.dbits] = 0;

  queue<Stat> q;
  q.push(st);
  int ans = -1;
  
  while (! q.empty()) {
    Stat u = q.front(); q.pop();

    int uch = flds[u.y][u.x];
    if (uch == '1' + u.k) {
      if (++u.k >= n) {
	ans = u.d;
	break;
      }
      dists[u.k][u.y][u.x][u.dbits] = u.d;
    }

    for (int di = 0; di < 4; di++) {
      Stat v = u;
      if (move(v, h, w, di) &&
	  dists[v.k][v.y][v.x][v.dbits] > v.d) {
	dists[v.k][v.y][v.x][v.dbits] = v.d;
	q.push(v);
      }
    }
  }

  printf("%d\n", ans);
  return 0;
}