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;
int n, m, xcs[20], ycs[20], maxlen, pd, xaim, yaim, g, pd2, js, js1, js2, js3,
    js4, maxnum;
int transx[8] = {1, -1, 0, 0, -1, -1, 1, 1},
    transy[8] = {0, 0, 1, -1, -1, 1, -1, 1},
    change[10] = {0, 0, 1, 3, 7, 15, 31, 63, 127, 255};
int dx[500000], dy[500000], xt[500000], yt[500000], xzf[500000], yzf[500000],
    dfx[500000], dsize, dnum[500000];
char c[17][17], d[17][17];
bool vis[17][17][65537];
void rebuild(int step, int x, int y, int Xt, int Yt, int Xzf, int Yzf) {
  if (step != 1) {
    c[x][y] = maxlen - step + 49;
    rebuild(step - 1, x + (Xt & 1) * (((Xzf & 1) << 1) - 1),
            y + (Yt & 1) * (((Yzf & 1) << 1) - 1), (Xt >> 1), (Yt >> 1),
            (Xzf >> 1), (Yzf >> 1));
  }
}
void redelete(int step, int x, int y, int Xt, int Yt, int Xzf, int Yzf) {
  if (step != 1) {
    c[x][y] = '.';
    redelete(step - 1, x + (Xt & 1) * (((Xzf & 1) << 1) - 1),
             y + (Yt & 1) * (((Yzf & 1) << 1) - 1), (Xt >> 1), (Yt >> 1),
             (Xzf >> 1), (Yzf >> 1));
  }
}
void drebuild(int step, int x, int y, int Xt, int Yt, int Xzf, int Yzf) {
  if (step != 0) {
    d[x][y] = maxlen - step + 49;
    drebuild(step - 1, x + (Xt & 1) * (((Xzf & 1) << 1) - 1),
             y + (Yt & 1) * (((Yzf & 1) << 1) - 1), (Xt >> 1), (Yt >> 1),
             (Xzf >> 1), (Yzf >> 1));
  }
}
void dredelete(int step, int x, int y, int Xt, int Yt, int Xzf, int Yzf) {
  if (step != 0) {
    d[x][y] = '.';
    dredelete(step - 1, x + (Xt & 1) * (((Xzf & 1) << 1) - 1),
              y + (Yt & 1) * (((Yzf & 1) << 1) - 1), (Xt >> 1), (Yt >> 1),
              (Xzf >> 1), (Yzf >> 1));
  }
}
int main(int argc, char *argv[]) {
  while (scanf("%d%d", &n, &m) != EOF) {
    g++;
    maxlen = dsize = xt[0] = yt[0] = xzf[0] = yzf[0] = pd = pd2 = dnum[0] = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        cin >> c[i][j];
        if (c[i][j] == '@') {
          xaim = i;
          yaim = j;
        }
        if (c[i][j] >= '1' && c[i][j] <= '9') {
          xcs[c[i][j] - 48] = i;
          ycs[c[i][j] - 48] = j;
          maxlen = max(maxlen, int(c[i][j]));
          c[i][j] = '.';
        }
      }
    maxnum = 1;
    for (int i = 1; i < maxlen; i++) maxnum <<= 2;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        for (int k = 0; k <= maxnum; k++) vis[i][j][k] = 0;
    for (int i = 0; i <= n + 1; i++) {
      c[i][0] = '#';
      c[i][m + 1] = '#';
    }
    for (int i = 0; i <= m + 1; i++) {
      c[0][i] = '#';
      c[n + 1][i] = '#';
    }
    for (int i = 0; i <= n + 1; i++)
      for (int j = 0; j <= m + 1; j++) d[i][j] = c[i][j];
    maxlen -= 48;
    dx[0] = xcs[1];
    dy[0] = ycs[1];
    for (int i = maxlen - 1; i > 0; i--) {
      xt[0] <<= 1;
      if (xcs[i + 1] != xcs[i]) xt[0] += 1;
      yt[0] <<= 1;
      if (ycs[i + 1] != ycs[i]) yt[0] += 1;
      xzf[0] = (xzf[0] << 1) + ((xcs[i + 1] - xcs[i] + 1) >> 1);
      yzf[0] = (yzf[0] << 1) + ((ycs[i + 1] - ycs[i] + 1) >> 1);
    }
    rebuild(maxlen, dx[0], dy[0], xt[0], yt[0], xzf[0], yzf[0]);
    js = 0;
    js1 = xt[0];
    js2 = xzf[0];
    js3 = yt[0];
    js4 = yzf[0];
    for (int i = 1; i < maxlen; i++) {
      js <<= 2;
      if ((js1 & 1) == 1) {
        if ((js2 & 1) == 1) js = js + 1;
      } else {
        if ((js4 & 1) == 0)
          js = js + 2;
        else
          js = js + 3;
      }
      js1 >>= 2;
      js2 >>= 2;
      js3 >>= 2;
      js4 >>= 2;
    }
    vis[xcs[1]][ycs[1]][js] = 1;
    redelete(maxlen, dx[0], dy[0], xt[0], yt[0], xzf[0], yzf[0]);
    for (int i = 0; i <= dsize; i++) {
      if (c[dx[i]][dy[i]] == '@') {
        printf("%d\n", dnum[i]);
        pd = 1;
        break;
      }
      rebuild(maxlen, dx[i], dy[i], xt[i], yt[i], xzf[i], yzf[i]);
      for (int j = 0; j < 4; j++)
        if (c[dx[i] + transx[j]][dy[i] + transy[j]] == '.' ||
            c[dx[i] + transx[j]][dy[i] + transy[j]] == '@') {
          dsize++;
          dnum[dsize] = dnum[i] + 1;
          dx[dsize] = dx[i] + transx[j];
          dy[dsize] = dy[i] + transy[j];
          xt[dsize] = (xt[i] << 1);
          if (dx[i] != dx[dsize]) xt[dsize] += 1;
          xt[dsize] &= change[maxlen];
          yt[dsize] = (yt[i] << 1);
          if (dy[i] != dy[dsize]) yt[dsize] += 1;
          yt[dsize] &= change[maxlen];
          xzf[dsize] = (xzf[i] << 1) + ((dx[i] - dx[dsize] + 1) >> 1);
          yzf[dsize] = (yzf[i] << 1) + ((dy[i] - dy[dsize] + 1) >> 1);
          drebuild(maxlen, dx[dsize], dy[dsize], xt[dsize], yt[dsize],
                   xzf[dsize], yzf[dsize]);
          js = 0;
          js1 = xt[dsize];
          js2 = xzf[dsize];
          js3 = yt[dsize];
          js4 = yzf[dsize];
          for (int i = 1; i < maxlen; i++) {
            js <<= 2;
            if ((js1 & 1) == 1) {
              if ((js2 & 1) == 1) js = js + 1;
            } else {
              if ((js4 & 1) == 0)
                js = js + 2;
              else
                js = js + 3;
            }
            js1 >>= 2;
            js2 >>= 2;
            js3 >>= 2;
            js4 >>= 2;
          }
          pd2 = 0;
          if (vis[dx[dsize]][dy[dsize]][js] == 1)
            pd2 = 1;
          else
            vis[dx[dsize]][dy[dsize]][js] = 1;
          dredelete(maxlen, dx[dsize], dy[dsize], xt[dsize], yt[dsize],
                    xzf[dsize], yzf[dsize]);
          if (pd2 == 1) dsize--;
        }
      redelete(maxlen, dx[i], dy[i], xt[i], yt[i], xzf[i], yzf[i]);
    }
    if (pd == 0) printf("-1\n");
  }
  return 0;
}