Problem: Let us remind you the rules of a very popular game called "Snake" (or sometimes "Boa", "Python" or "Worm").

The game field is represented by an n × m rectangular table. Some squares of the field are considered impassable (walls), all other squares of the fields are passable.

You control a snake, the snake consists of segments. Each segment takes up exactly one passable square of the field, but any passable square contains at most one segment. All segments are indexed by integers from 1 to k, where k is the snake's length. The 1-th segment is the head and the k-th segment is the tail. For any i (1 ≤ i < k), segments with indexes i and i + 1 are located in the adjacent squares of the field, that is, these squares share a common side.

One of the passable field squares contains an apple. The snake's aim is to reach the apple and eat it (that is, to position its head in the square with the apple).

The snake moves throughout the game. During one move the snake can move its head to an adjacent field square. All other segments follow the head. That is, each segment number i (1 < i ≤ k) moves to the square that has just had segment number i - 1. Consider that all segments including the head move simultaneously (see the second test sample). If the snake's head moves to an unpassable square or to the square, occupied by its other segment, the snake dies. That's why we will consider such moves unvalid.

Your task is to determine the minimum number of valid moves that the snake needs to reach the apple.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 15) — the number of rows and columns of the game field.

Next n lines describe the game field. Each of these lines contains m characters. Character "#" represents a wall, "." is a passable square, "@" is an apple. The snake's first segment is represented by character "1", the second one segment — by character "2" and so on.

The game field description doesn't contain any characters besides "#', ".", "@" and digits (except 0). It is guaranteed that the described field is correct. It is guaranteed that the described field contains exactly one apple and exactly one snake, the snake's length is at least 3 and at most 9.

Output

Print a single integer to the output — the minimum number of moves needed to reach the apple. If the snake can't reach the apple, print -1.

Examples

Input

4 5
##...
..1#@
432#.
...#.


Output

4


Input

4 4
#78#
.612
.543
..@.


Output

6


Input

3 2
3@
2#
1#


Output

-1

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int Inf = 2000000000;
const int Maxn = 15;
const int Maxm = 14745700;
const int Maxl = 10;
const int Maxd = 4;
const int dy[Maxd] = {-1, 0, 1, 0};
const int dx[Maxd] = {0, 1, 0, -1};
struct board {
  char m[Maxn][Maxn];
};
int n, m;
board B;
int len;
int fy, fx;
int mod;
int dist[Maxm];
int res = -1;
int mx;
int toInt(const board &B) {
  int r[Maxl], c[Maxl];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (isdigit(B.m[i][j])) {
        r[B.m[i][j] - '0'] = i;
        c[B.m[i][j] - '0'] = j;
      }
  int res = 0;
  for (int i = len; i >= 2; i--) {
    res *= Maxd;
    int j = 0;
    while (r[i - 1] + dy[j] != r[i] || c[i - 1] + dx[j] != c[i]) j++;
    res += j;
  }
  res *= Maxn;
  res += r[1];
  res *= Maxn;
  res += c[1];
  return res;
}
board toBoard(int p) {
  board nB;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) nB.m[i][j] = B.m[i][j] == '#' ? '#' : '.';
  int j = p % Maxn;
  p /= Maxn;
  int i = p % Maxn;
  p /= Maxn;
  char dig = '1';
  nB.m[i][j] = dig;
  for (int l = 2; l <= len; l++) {
    int d = p % Maxd;
    p /= Maxd;
    i += dy[d];
    j += dx[d];
    dig++;
    nB.m[i][j] = dig;
  }
  return nB;
}
board Move(const board &B, int ni, int nj) {
  board nB;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (B.m[i][j] == '#')
        nB.m[i][j] = '#';
      else if (i == ni && j == nj)
        nB.m[i][j] = '1';
      else if (isdigit(B.m[i][j]) && B.m[i][j] < '0' + len)
        nB.m[i][j] = B.m[i][j] + 1;
      else
        nB.m[i][j] = '.';
  return nB;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      scanf(" %c", &B.m[i][j]);
      if (B.m[i][j] == '@') {
        fy = i;
        fx = j;
        B.m[i][j] = '.';
      } else if (isdigit(B.m[i][j]))
        len = max(len, B.m[i][j] - '0');
    }
  mod = Maxn * Maxn;
  for (int i = 0; i < len - 2; i++) mod *= Maxd;
  int v = toInt(B);
  fill(dist, dist + Maxm, Inf);
  dist[v] = 0;
  queue<int> Q;
  Q.push(v);
  while (!Q.empty()) {
    v = Q.front();
    int d = dist[v];
    Q.pop();
    board nB = toBoard(v);
    int j = v % Maxn, i = v / Maxn % Maxn;
    if (i == fy && j == fx) {
      res = d;
      break;
    }
    for (int k = 0; k < Maxd; k++) {
      int ni = i + dy[k], nj = j + dx[k];
      if (0 <= ni && ni < n && 0 <= nj && nj < m &&
          (nB.m[ni][nj] == '.' || nB.m[ni][nj] == '0' + len)) {
        int u = v % mod;
        u = u / Maxn / Maxn;
        u *= Maxd;
        u += (k + 2) % Maxd;
        u *= Maxn;
        u += ni;
        u *= Maxn;
        u += nj;
        if (d + 1 < dist[u]) {
          dist[u] = d + 1;
          Q.push(u);
        }
      }
    }
  }
  printf("%d\n", res);
  return 0;
}