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>
#pragma GCC optimize(1)
#pragma G++ optimize(1)
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#pragma GCC optimize(3)
#pragma G++ optimize(3)
#pragma GCC optimize(4)
#pragma G++ optimize(4)
#pragma GCC optimize(5)
#pragma G++ optimize(5)
#pragma GCC optimize(6)
#pragma G++ optimize(6)
#pragma GCC optimize(7)
#pragma G++ optimize(7)
#pragma GCC optimize(8)
#pragma G++ optimize(8)
#pragma GCC optimize(9)
#pragma G++ optimize(9)
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
using namespace std;
const int INF = 1e9;
const int maxn = 10005;
struct bign {
  int d[maxn], len;
  inline void clean() {
    while (len > 1 && !d[len - 1]) len--;
  }
  inline bign() {
    memset(d, 0, sizeof(d));
    len = 1;
  }
  inline bign(int num) { *this = num; }
  inline bign(char* num) { *this = num; }
  inline bign operator=(const char* num) {
    memset(d, 0, sizeof(d));
    len = strlen(num);
    for (int i = 0; i < len; i++) d[i] = num[len - 1 - i] - '0';
    clean();
    return *this;
  }
  inline bign operator=(int num) {
    char s[20];
    sprintf(s, "%d", num);
    *this = s;
    return *this;
  }
  inline bign operator+(const bign& b) {
    bign c = *this;
    int i;
    for (i = 0; i < b.len; i++) {
      c.d[i] += b.d[i];
      if (c.d[i] > 9) c.d[i] %= 10, c.d[i + 1]++;
    }
    while (c.d[i] > 9) c.d[i++] %= 10, c.d[i]++;
    c.len = max(len, b.len);
    if (c.d[i] && c.len <= i) c.len = i + 1;
    return c;
  }
  inline bign operator-(const bign& b) {
    bign c = *this;
    int i;
    for (i = 0; i < b.len; i++) {
      c.d[i] -= b.d[i];
      if (c.d[i] < 0) c.d[i] += 10, c.d[i + 1]--;
    }
    while (c.d[i] < 0) c.d[i++] += 10, c.d[i]--;
    for (i = c.len - 1; i > 0; i--)
      if (c.d[i] != 0) break;
    c.len = i + 1;
    return c;
  }
  inline bign operator*(const bign& b) const {
    int i, j;
    bign c;
    c.len = len + b.len;
    for (j = 0; j < b.len; j++)
      for (i = 0; i < len; i++) c.d[i + j] += d[i] * b.d[j];
    for (i = 0; i < c.len - 1; i++) c.d[i + 1] += c.d[i] / 10, c.d[i] %= 10;
    c.clean();
    return c;
  }
  inline bign operator/(const bign& b) {
    int i, j;
    bign c = *this, a = 0;
    for (i = len - 1; i >= 0; i--) {
      a = a * 10 + d[i];
      for (j = 0; j < 10; j++)
        if (a < b * (j + 1)) break;
      c.d[i] = j;
      a = a - b * j;
    }
    c.clean();
    return c;
  }
  inline bign operator%(const bign& b) {
    int i, j;
    bign a = 0;
    for (i = len - 1; i >= 0; i--) {
      a = a * 10 + d[i];
      for (j = 0; j < 10; j++)
        if (a < b * (j + 1)) break;
      a = a - b * j;
    }
    return a;
  }
  inline bign operator+=(const bign& b) {
    *this = *this + b;
    return *this;
  }
  inline bign operator*=(const bign& b) {
    *this = *this * b;
    return *this;
  }
  inline bign operator-=(const bign& b) {
    *this = *this - b;
    return *this;
  }
  inline bign operator/=(const bign& b) {
    *this = *this / b;
    return *this;
  }
  inline bool operator<(const bign& b) const {
    if (len != b.len) return len < b.len;
    for (int i = len - 1; i >= 0; i--)
      if (d[i] != b.d[i]) return d[i] < b.d[i];
    return false;
  }
  inline bool operator>(const bign& b) const { return b < *this; }
  inline bool operator<=(const bign& b) const { return !(b < *this); }
  inline bool operator>=(const bign& b) const { return !(*this < b); }
  inline bool operator!=(const bign& b) const { return b < *this || *this < b; }
  inline bool operator==(const bign& b) const {
    return !(b < *this) && !(b > *this);
  }
  inline string str() const {
    char s[maxn] = {};
    for (int i = 0; i < len; i++) s[len - 1 - i] = d[i] + '0';
    return s;
  }
};
inline istream& operator>>(istream& in, bign& x) {
  string s;
  in >> s;
  x = s.c_str();
  return in;
}
inline ostream& operator<<(ostream& out, const bign& x) {
  out << x.str();
  return out;
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline int pt(int a[], int l, int r) {
  int p, i, j;
  p = a[l];
  i = l;
  j = r + 1;
  for (;;) {
    while (a[++i] < p)
      if (i >= r) break;
    while (a[--j] > p)
      if (j <= l) break;
    if (i >= j)
      break;
    else
      swap(a[i], a[j]);
  }
  if (j == l) return j;
  swap(a[l], a[j]);
  return j;
}
inline void q_sort(int a[], int l, int r) {
  int q;
  if (r > l) {
    q = pt(a, l, r);
    q_sort(a, l, q - 1);
    q_sort(a, q + 1, r);
  }
}
inline void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline void rd(long long& val) {
  long long x = 0;
  int f = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  val = x * f;
}
inline void rd(int& val) {
  int x = 0;
  int f = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  val = x * f;
}
inline void print(int x) {
  write(x);
  putchar('\n');
}
pair<int, int> t[11];
int n, m, x, y;
pair<short, short> apple;
char c[22][22];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
map<deque<pair<short, short> >, bool> dp;
inline bool check(short i, deque<pair<short, short> > deq) {
  pair<short, short> frnt;
  frnt = deq.front();
  short xx, yy;
  xx = frnt.first + dx[i];
  yy = frnt.second + dy[i];
  if (xx < 1 || xx == n + 1 || yy < 1 || yy == m + 1) return 0;
  frnt = make_pair(xx, yy);
  deq.pop_front();
  while (deq.size() > 1) {
    if (deq.front() == frnt) return 0;
    deq.pop_front();
  }
  return 1;
}
inline short bfs(deque<pair<short, short> > deq) {
  queue<pair<deque<pair<short, short> >, short> > qd;
  pair<deque<pair<short, short> >, short> ped;
  deque<pair<short, short> > dep, tdep;
  pair<short, short> frnt;
  short cost, last;
  qd.push(make_pair(deq, 0));
  while (!qd.empty()) {
    ped = qd.front();
    qd.pop();
    cost = ped.second;
    dep = ped.first;
    if (dp[dep]) continue;
    if (dep.front() == apple) {
      return cost;
    }
    frnt = dep.front();
    for (short i = 0; i < 4; i++) {
      if (check(i, dep)) {
        if (c[frnt.first + dx[i]][frnt.second + dy[i]] != '#') {
          tdep = dep;
          tdep.pop_back();
          tdep.push_front(make_pair(frnt.first + dx[i], frnt.second + dy[i]));
          qd.push(make_pair(tdep, cost + 1));
        }
      }
    }
    dp[dep] = 1;
  }
  return -1;
}
inline void tanxin() {
  int a, b, i, j;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (c[i][j] == '1') {
        a = i;
        b = j;
        goto end;
      }
    }
  }
end:;
  int cnt = 0;
  while (a > x) {
    a--;
    cnt++;
    if (c[a][b] != '.' && c[a][b] != '@') return;
  }
  while (a < x) {
    a++;
    cnt++;
    if (c[a][b] != '.' && c[a][b] != '@') return;
  }
  while (b > y) {
    b--;
    cnt++;
    if (c[a][b] != '.' && c[a][b] != '@') return;
  }
  while (b < y) {
    b++;
    cnt++;
    if (c[a][b] != '.' && c[a][b] != '@') return;
  }
  cout << cnt << endl;
  exit(0);
}
int can;
bool use[22][22];
inline void dfs(int a, int b) {
  if (a == 0 || a == n + 1 || b == 0 || b == m + 1) return;
  if (use[a][b]) return;
  if (c[a][b] >= '1' && c[a][b] <= '9') {
    can = 1;
    return;
  }
  if (c[a][b] != '.' && c[a][b] != '@') return;
  use[a][b] = 1;
  for (int i = 0; i < 4; i++) {
    dfs(a + dx[i], b + dy[i]);
    if (can) return;
  }
}
inline bool canarrive() {
  can = 0;
  dfs(x, y);
  if (can)
    return 1;
  else
    return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  short i, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> c[i][j];
      if (c[i][j] >= '1' && c[i][j] <= '9') t[c[i][j] - '1'] = make_pair(i, j);
      if (c[i][j] == '@') {
        x = i;
        y = j;
        apple = make_pair(x, y);
      }
    }
  }
  tanxin();
  if (!canarrive()) {
    cout << -1 << endl;
    return 0;
  }
  deque<pair<short, short> > deq;
  for (i = 0; i < 9; i++) {
    if (t[i] != make_pair(0, 0))
      deq.push_back(t[i]);
    else
      break;
  }
  cout << bfs(deq) << endl;
  return 0;
}