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 maxn = 17, maxmask = 66000;
int n, m, k;
int headx, heady;
char grid[maxn][maxn];
char ngrid[maxn][maxn], curgrid[maxn][maxn];
int tmp[20];
int used[16][16][maxmask];
struct state {
  int hx, hy, m;
};
queue<state> q;
void read() {
  cin >> n >> m;
  for (int i = 0; i < maxn; ++i)
    for (int j = 0; j < maxn; ++j) grid[i][j] = '#';
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      do {
        scanf("%c", &grid[i][j]);
        if (grid[i][j] >= '1' && grid[i][j] <= '9') {
          k = max(k, grid[i][j] - '0');
          break;
        }
      } while (grid[i][j] != '.' && grid[i][j] != '#' && grid[i][j] != '@');
      if (grid[i][j] == '1') {
        headx = i;
        heady = j;
      }
    }
}
int init() {
  int pos = 0;
  int cur = 1;
  int x = headx, y = heady;
  while (cur < k) {
    if (grid[x + 1][y] - '0' == cur + 1) {
      tmp[pos] = 0;
      tmp[pos + 1] = 0;
      x++;
      pos += 2;
      cur = grid[x][y] - '0';
    } else if (grid[x][y - 1] - '0' == cur + 1) {
      tmp[pos] = 1;
      tmp[pos + 1] = 0;
      y--;
      pos += 2;
      cur = grid[x][y] - '0';
    } else if (grid[x - 1][y] - '0' == cur + 1) {
      tmp[pos] = 0;
      tmp[pos + 1] = 1;
      x--;
      pos += 2;
      cur = grid[x][y] - '0';
    } else if (grid[x][y + 1] - '0' == cur + 1) {
      tmp[pos] = 1;
      tmp[pos + 1] = 1;
      y++;
      pos += 2;
      cur = grid[x][y] - '0';
    }
  }
  int pw = 1, ret = 0;
  for (int i = pos - 1; i >= 0; --i) {
    if (i < pos - 1) pw *= 2;
    ret += tmp[i] * pw;
  }
  return ret;
}
int fun(int xheadx, int yheady) {
  int pos = 0;
  int cur = 1;
  int x = xheadx, y = yheady;
  while (cur < k) {
    if (curgrid[x + 1][y] - '0' == cur + 1) {
      tmp[pos] = 0;
      tmp[pos + 1] = 0;
      x++;
      pos += 2;
      cur = curgrid[x][y] - '0';
    } else if (curgrid[x][y - 1] - '0' == cur + 1) {
      tmp[pos] = 1;
      tmp[pos + 1] = 0;
      y--;
      pos += 2;
      cur = curgrid[x][y] - '0';
    } else if (curgrid[x - 1][y] - '0' == cur + 1) {
      tmp[pos] = 0;
      tmp[pos + 1] = 1;
      x--;
      pos += 2;
      cur = curgrid[x][y] - '0';
    } else if (curgrid[x][y + 1] - '0' == cur + 1) {
      tmp[pos] = 1;
      tmp[pos + 1] = 1;
      y++;
      pos += 2;
      cur = curgrid[x][y] - '0';
    }
  }
  int pw = 1, ret = 0;
  for (int i = pos - 1; i >= 0; --i) {
    if (i < pos - 1) pw *= 2;
    ret += tmp[i] * pw;
  }
  return ret;
}
int nextx[6];
int nexty[6];
int bfs() {
  int mask = init();
  used[headx][heady][mask] = 1;
  state st, nst;
  st.hx = headx;
  st.hy = heady;
  st.m = mask;
  q.push(st);
  nextx[0] = 1;
  nextx[1] = 0;
  nextx[2] = -1;
  nextx[3] = 0;
  nexty[0] = 0;
  nexty[1] = -1;
  nexty[2] = 0;
  nexty[3] = 1;
  int curx, cury, curnum, po, posx, posy;
  while (!q.empty()) {
    st = q.front();
    q.pop();
    curx = st.hx;
    cury = st.hy;
    if (grid[curx][cury] == '@') return used[curx][cury][st.m];
    for (int i = 0; i < maxn; ++i)
      for (int j = 0; j < maxn; ++j)
        if (grid[i][j] == '.' || grid[i][j] == '#' || grid[i][j] == '@')
          ngrid[i][j] = grid[i][j];
        else
          ngrid[i][j] = '.';
    ngrid[curx][cury] = '1';
    curnum = 1;
    po = (k - 1) * 2;
    for (int i = 0; i <= 2 * (k - 1); ++i) {
      tmp[po] = (st.m >> i) & 1;
      po--;
    }
    po = 1;
    posx = curx;
    posy = cury;
    int curpos;
    while (curnum < k) {
      curpos = tmp[po] * 1;
      curpos += tmp[po + 1] * 2;
      po += 2;
      if (curpos == 0) {
        posx += 1;
        ngrid[posx][posy] = char('0' + curnum + 1);
        curnum++;
      } else if (curpos == 1) {
        posy -= 1;
        ngrid[posx][posy] = char('0' + curnum + 1);
        curnum++;
      } else if (curpos == 2) {
        posx -= 1;
        ngrid[posx][posy] = char('0' + curnum + 1);
        curnum++;
      } else if (curpos == 3) {
        posy += 1;
        ngrid[posx][posy] = char('0' + curnum + 1);
        curnum++;
      }
    }
    int newx, newy, newmask;
    for (int ii = 0; ii < 4; ii++) {
      newx = curx + nextx[ii];
      newy = cury + nexty[ii];
      if (ngrid[newx][newy] == '.' || ngrid[newx][newy] == '@' ||
          ngrid[newx][newy] == char('0' + k)) {
        for (int i = 0; i < maxn; ++i)
          for (int j = 0; j < maxn; ++j) curgrid[i][j] = ngrid[i][j];
        for (int i = 1; i <= n; ++i)
          for (int j = 1; j <= m; ++j) {
            if (curgrid[i][j] >= '1' && curgrid[i][j] <= '9') {
              if (curgrid[i][j] == char('0' + k))
                curgrid[i][j] = '.';
              else
                curgrid[i][j] = char((curgrid[i][j]) + 1);
            }
          }
        curgrid[newx][newy] = '1';
        newmask = fun(newx, newy);
        if (used[newx][newy][newmask] == 0) {
          used[newx][newy][newmask] = used[curx][cury][st.m] + 1;
          nst.hx = newx;
          nst.hy = newy;
          nst.m = newmask;
          q.push(nst);
        }
      }
    }
  }
  return 0;
}
void solve() { cout << bfs() - 1 << endl; }
int main() {
  read();
  solve();
  return 0;
}