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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <bitset>

#ifdef LOCAL
#include "local.h"
#endif

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define clr(a, x) memset(a, x, sizeof(a))
#define sz(a) ((int)(a).size())
#define mp(a, b) make_pair(a, b)
#define ten(n) ((long long)(1e##n))

template <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }
template <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }
template <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }
template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }
template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }
void fast_io() { cin.tie(0); ios::sync_with_stdio(false); }
bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }

typedef long long ll;
typedef pair<int, int> pint;

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


int main()
{
    int h, w, n;
    cin >> h >> w >> n;
    char c[16][16];
    rep(y, h)
        cin >> c[y];

    vector<pint> start_imo(6);
    const char* imoimo = "Sabcde";
    rep(y, h) rep(x, w)
    {
        if (isalpha(c[y][x]))
            start_imo[strchr(imoimo, c[y][x]) - imoimo] = pint(x, y);
    }

    int res = -1;
    map<pair<int, vector<pint>>, int> dp;
    queue<pair<int, vector<pint>>> q;
    q.push(make_pair(0, start_imo));
    while (!q.empty())
    {
        const int esa = q.front().first;
        const vector<pint> imo = q.front().second;
        const int ncost = dp[q.front()] + 1;
        q.pop();
        assert(imo.size() == 6);

        rep(dir, 4)
        {
            int nx = imo[0].first + dx[dir];
            int ny = imo[0].second + dy[dir];
            if (in_rect(nx, ny, w, h) && c[ny][nx] != '#' &&
                count(imo.begin() + 1, imo.begin() + 5, pint(nx, ny)) == 0)
            {
                vector<pint> nimo;
                nimo.push_back(pint(nx, ny));
                rep(i, 5)
                    nimo.push_back(imo[i]);

                int nesa = esa;
                if (c[ny][nx] - '1' == esa)
                    ++nesa;

                if (nesa == n)
                {
                    res = ncost;
                    goto end;
                }

                auto next = make_pair(nesa, nimo);
                if (!dp.count(next))
                {
                    dp[next] = ncost;
                    q.push(next);
                }
            }
        }
    }
end:;
    cout << res << endl;
}