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;
void debug_out() { cerr << '\n'; }
template <typename T, typename... R>
void debug_out(const T &f, const R &...r) {
  cerr << f << " ";
  debug_out(r...);
}
inline long long read() {
  long long x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x = f ? -x : x;
}
inline void write(long long x) {
  if (x == 0) {
    putchar('0');
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  static char s[23];
  int l = 0;
  while (x != 0) s[l++] = x % 10 + 48, x /= 10;
  while (l) putchar(s[--l]);
}
int lowbit(int x) { return x & (-x); }
template <class T>
T big(const T &a1, const T &a2) {
  return a1 > a2 ? a1 : a2;
}
template <typename T, typename... R>
T big(const T &f, const R &...r) {
  return big(f, big(r...));
}
template <class T>
T sml(const T &a1, const T &a2) {
  return a1 < a2 ? a1 : a2;
}
template <typename T, typename... R>
T sml(const T &f, const R &...r) {
  return sml(f, sml(r...));
}
template <class T, class U>
inline void checkMin(T &x, U y) {
  if (y < x) x = y;
}
template <class T, class U>
inline void checkMax(T &x, U y) {
  if (y > x) x = y;
}
template <class T, class U>
inline bool ifMax(T &x, U y) {
  if (y > x) return x = y, true;
  return false;
}
template <class T, class U>
inline bool ifMin(T &x, U y) {
  if (y < x) return x = y, true;
  return false;
}
const int M = 212345;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1000000007;
char mp[15][15];
int snake[15][15][1u << 17u];
int to[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int n, m;
struct node {
  int x, y;
} head, app;
struct node2 {
  int x, y;
  unsigned bits;
  int step;
} now, nex;
void BinaryRecursion(int n) {
  int a;
  a = n % 2;
  n = n >> 1;
  if (n == 0)
    ;
  else
    BinaryRecursion(n);
  cout << a;
}
int len;
void getS(int nowX, int nowY, int nxt, unsigned nowBits) {
  if (nowX != 0 && mp[nowX - 1][nowY] == '0' + nxt)
    getS(nowX - 1, nowY, nxt + 1, (nowBits << 1u) << 1u);
  else if (nowY != 0 && mp[nowX][nowY - 1] == '0' + nxt)
    getS(nowX, nowY - 1, nxt + 1, ((nowBits << 1u) << 1u) + 1u);
  else if (nowX < n && mp[nowX + 1][nowY] == '0' + nxt)
    getS(nowX + 1, nowY, nxt + 1, ((nowBits << 1u) + 1u) << 1u);
  else if (nowY < m && mp[nowX][nowY + 1] == '0' + nxt)
    getS(nowX, nowY + 1, nxt + 1, (((nowBits << 1u) + 1u) << 1u) + 1u);
  else {
    len = nxt - 1;
    now.x = head.x, now.y = head.y, now.bits = nowBits;
    return;
  }
}
int vis[15][15];
bool hit(int x, int y, int bfx, int bfy, unsigned bits) {
  if (x < 0 || y < 0 || x >= n || y >= m) return true;
  if (mp[x][y] == '#') return true;
  set<pair<int, int> > ocu;
  int nowX = bfx, nowY = bfy;
  stack<pair<int, int> > s;
  int cnt = 0;
  while (cnt < len - 1) {
    unsigned t2 = bits & 1u;
    bits >>= 1u;
    unsigned t1 = bits & 1u;
    bits >>= 1u;
    s.push({t1, t2});
    cnt++;
  }
  if (s.empty()) return false;
  unsigned tot;
  (s.size() == 1) ? (tot = s.size()) : (tot = s.size() - 1);
  while (tot) {
    tot--;
    pair<int, int> tmp = s.top();
    if (tmp.first == 1 && tmp.second == 1)
      nowY++;
    else if (tmp.first == 1 && tmp.second == 0)
      nowX++;
    else if (tmp.first == 0 && tmp.second == 1)
      nowY--;
    else if (tmp.first == 0 && tmp.second == 0)
      nowX--;
    ocu.insert({nowX, nowY});
    s.pop();
  }
  if (ocu.count({x, y})) return true;
  return false;
}
int bfs() {
  queue<node2> q;
  q.push(now);
  vis[now.x][now.y] = 1;
  while (!q.empty()) {
    if (now.step > 2000) {
      return -1;
    }
    now = q.front();
    q.pop();
    vis[now.x][now.y] = 0;
    if (mp[now.x][now.y] == '@') {
      return now.step;
    }
    for (int i = 0; i < 4; i++) {
      nex.x = now.x + to[i][0], nex.y = now.y + to[i][1],
      nex.step = now.step + 1, nex.bits = now.bits >> 2u;
      if (len != 1)
        nex.bits += (((i == 1 || i == 3) ? 1u : 0u))
                    << ((unsigned)(len - 1) * 2u - 1u);
      if (len != 1)
        nex.bits += (((i == 0 || i == 1) ? 1u : 0u))
                    << ((unsigned)(len - 1) * 2u - 2u);
      if (hit(nex.x, nex.y, now.x, now.y, now.bits)) continue;
      if (vis[nex.x][nex.y]) continue;
      q.push(nex);
      vis[nex.x][nex.y] = 1;
    }
  }
  return -1;
}
void init() {
  n = read(), m = read();
  for (int i = 0; i < n; ++i) {
    scanf("%s", mp[i]);
    for (int j = 0; j < m; ++j) {
      if (mp[i][j] == '1') {
        head.x = i, head.y = j;
      } else if (mp[i][j] == '@') {
        app.x = i, app.y = j;
      }
    }
  }
  getS(head.x, head.y, 2, 0u);
  write(bfs());
  puts("");
}
void solve() {}
int main() {
  init();
  solve();
  return 0;
}