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 dr[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, -1, 1};
const double eps = 1e-9;
const int INF = 0x7FFFFFFF;
const long long INFLL = 0x7FFFFFFFFFFFFFFFLL;
const double pi = acos(-1);
template <class T>
T take(queue<T> &O) {
  T tmp = O.front();
  O.pop();
  return tmp;
}
template <class T>
T take(stack<T> &O) {
  T tmp = O.top();
  O.pop();
  return tmp;
}
template <class T>
T take(priority_queue<T> &O) {
  T tmp = O.top();
  O.pop();
  return tmp;
}
inline void getint(int &num) { scanf("%d", &num); }
inline void getint(long long &num) { cin >> num; }
int dist[1 << 16][15][15];
int r, c;
char area[20][20];
int pos[10][2], mask, mx;
queue<pair<int, pair<int, int> > > Q;
queue<int> D;
inline bool safe(pair<int, int> x) {
  return x.first >= 0 && x.first < r && x.second >= 0 && x.second < c &&
         area[x.first][x.second] != '#';
}
int main() {
  scanf("%d%d\n", &r, &c);
  for (int(i) = (0), _t = (r); i < (_t); ++(i)) {
    gets(area[i]);
    for (int(j) = (0), _t = (c); j < (_t); ++(j))
      if (isdigit(area[i][j])) {
        pos[area[i][j] - '0'][0] = i;
        pos[area[i][j] - '0'][1] = j;
        mx = max(mx, area[i][j] - '0');
      }
  }
  for (int(i) = (0), _t = (1 << 16); i < (_t); ++(i))
    for (int(j) = (0), _t = (15); j < (_t); ++(j))
      for (int(k) = (0), _t = (15); k < (_t); ++(k)) dist[i][j][k] = INF - 1;
  for (int(k) = (mx), _t = (2); k >= (_t); --(k))
    for (int(i) = (0), _t = (4); i < (_t); ++(i))
      if (pos[k - 1][0] + dr[i] == pos[k][0] &&
          pos[k - 1][1] + dc[i] == pos[k][1]) {
        mask = (mask << 2) + i;
        break;
      }
  int tmp = mask;
  {}
  for (int(i) = (1), _t = (mx); i < (_t); ++(i)) {
    {};
    tmp >>= 2;
  }
  {};
  {};
  dist[mask][pos[1][0]][pos[1][1]] = 0;
  Q.push(make_pair(mask, make_pair(pos[1][0], pos[1][1])));
  D.push(0);
  while (!Q.empty()) {
    pair<int, pair<int, int> > cur = Q.front();
    int cdist = D.front();
    Q.pop();
    D.pop();
    {};
    for (int(i) = (0), _t = (4); i < (_t); ++(i)) {
      pair<int, int> n =
          make_pair(cur.second.first + dr[i], cur.second.second + dc[i]);
      int tmask = cur.first;
      if (!safe(n)) continue;
      int nmask = cur.first & ((1 << ((mx - 1) * 2 - 2)) - 1);
      nmask <<= 2;
      nmask |= (i + 2) % 4;
      int tmpn = nmask;
      {}
      for (int(z) = (1), _t = (mx); z < (_t); ++(z)) {
        {};
        tmpn >>= 2;
      }
      {};
      {};
      {};
      if (cdist + 1 >= dist[nmask][n.first][n.second]) continue;
      pair<int, int> tmp = cur.second;
      bool gagal = 0;
      for (int(k) = (0), _t = (mx - 2); k < (_t); ++(k)) {
        {};
        if (n.first == tmp.first + dr[tmask & 3] &&
            n.second == tmp.second + dc[tmask & 3]) {
          gagal = 1;
          break;
        } else {
          tmp.first += dr[tmask & 3];
          tmp.second += dc[tmask & 3];
          tmask >>= 2;
        }
      }
      if (gagal) continue;
      {};
      if (area[n.first][n.second] == '@') {
        printf("%d\n", cdist + 1);
        return 0;
      }
      dist[nmask][n.first][n.second] = cdist + 1;
      Q.push(make_pair(nmask, n));
      D.push(cdist + 1);
    }
  }
  puts("-1");
  return 0;
}