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>
namespace solution {
class SolutionInterface {
 public:
  virtual int run() = 0;

 protected:
  virtual void pre_calc() {}
  virtual bool action() = 0;
  virtual void init(){};
  virtual bool input() { return false; };
  virtual void output(){};
  SolutionInterface() {}

 private:
};
}  // namespace solution
namespace solution {
class SolutionBase : public SolutionInterface {
 public:
  virtual int run() {
    pre_calc();
    while (action())
      ;
    return 0;
  }
};
}  // namespace solution
namespace io {
const std::string OUTPUT_VECTOR_DELIMITER = ",";
}
namespace io {
template <class A, class B>
std::ostream& operator<<(std::ostream& os, const std::pair<A, B>& p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <class A, class B>
std::istream& operator>>(std::istream& is, std::pair<A, B>& p) {
  return is >> p.first >> p.second;
}
}  // namespace io
namespace io {
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
  for (typename std::vector<T>::const_iterator it_i = v.begin();
       it_i != v.end(); ++it_i) {
    os << *it_i;
    if (it_i + 1 != v.end()) os << OUTPUT_VECTOR_DELIMITER;
  }
  return os;
}
template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
  int n = v.size();
  for (int i = 0; i < n; ++i) {
    is >> v[i];
  }
  return is;
}
}  // namespace io
namespace io {
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::deque<T>& v) {
  for (typename std::deque<T>::const_iterator it_i = v.begin(); it_i != v.end();
       ++it_i) {
    os << *it_i;
    if (it_i + 1 != v.end()) os << OUTPUT_VECTOR_DELIMITER;
  }
  return os;
}
template <typename T>
std::istream& operator>>(std::istream& is, std::deque<T>& v) {
  int n = v.size();
  for (int i = 0; i < n; ++i) {
    is >> v[i];
  }
  return is;
}
}  // namespace io
namespace io {
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::list<T>& v) {
  for (typename std::list<T>::const_iterator it_i = v.begin(); it_i != v.end();
       ++it_i) {
    os << *it_i;
    os << OUTPUT_VECTOR_DELIMITER << " ";
  }
  return os;
}
template <typename T>
std::istream& operator>>(std::istream& is, std::list<T>& v) {
  int n = v.size();
  for (int i = 0; i < n; ++i) {
    is >> v[i];
  }
  return is;
}
}  // namespace io
namespace solution {}
namespace solution {
using namespace std;
using namespace io;
}  // namespace solution
namespace solution {
const int SIZE = 15;
const int NONE = std::numeric_limits<int>::max();
const char CHAR_APPLE = '@';
const char CHAR_EMPTY = '.';
const char CHAR_WALL = '#';
const std::list<std::pair<int, int> > INVALID_SNAKE(1, std::pair<int, int>(-1,
                                                                           -1));
const int dr[4] = {1, -1, 0, 0};
const int dc[4] = {0, 0, 1, -1};
int H, W;
string S[SIZE];
std::list<std::pair<int, int> > start_snake;
int gr, gc;
std::queue<int> Q_steps;
std::queue<int> Q_r;
std::queue<int> Q_c;
std::queue<std::list<std::pair<int, int> > > Q_snake;
std::set<std::list<std::pair<int, int> > > visited;
int result;
}  // namespace solution
namespace solution {
class Solver {
 public:
  void solve() {
    normalize();
    result = get_minimum_steps();
  }
  void normalize() {
    int snake_len = 0;
    std::pair<int, int> snake_segments[SIZE];
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        char c = S[i][j];
        if (is_digit(c)) {
          int ind = c - '1';
          snake_segments[ind] = std::pair<int, int>(i, j);
          snake_len++;
          S[i][j] = CHAR_EMPTY;
        }
      }
    }
    start_snake.clear();
    for (int i = 0; i + 1 < snake_len; ++i) {
      start_snake.push_back(snake_segments[i]);
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (S[i][j] == CHAR_APPLE) {
          gr = i;
          gc = j;
          S[i][j] = CHAR_EMPTY;
        }
      }
    }
  }
  bool is_digit(char c) { return '0' <= c && c <= '9'; }
  int get_minimum_steps() {
    int res = NONE;
    Q_steps = std::queue<int>();
    Q_r = std::queue<int>();
    Q_c = std::queue<int>();
    Q_snake = std::queue<std::list<std::pair<int, int> > >();
    visited.clear();
    const std::pair<int, int>& start_snake_head = *start_snake.begin();
    Q_steps.push(0);
    Q_r.push(start_snake_head.first);
    Q_c.push(start_snake_head.second);
    Q_snake.push(start_snake);
    visited.insert(start_snake);
    while (!Q_steps.empty()) {
      int steps = Q_steps.front();
      Q_steps.pop();
      int r = Q_r.front();
      Q_r.pop();
      int c = Q_c.front();
      Q_c.pop();
      std::list<std::pair<int, int> > snake = Q_snake.front();
      Q_snake.pop();
      if (r == gr && c == gc) return steps;
      std::set<std::pair<int, int> > segments(snake.begin(), snake.end());
      for (int k = 0; k < 4; ++k) {
        int nr = r + dr[k];
        int nc = c + dc[k];
        std::pair<int, int> next_head(nr, nc);
        if (is_invalid_pos(nr, nc)) continue;
        if (segments.count(next_head)) continue;
        std::list<std::pair<int, int> > next_snake = snake;
        next_snake.push_front(next_head);
        next_snake.pop_back();
        int next_steps = steps + 1;
        if (visited.count(next_snake)) continue;
        visited.insert(next_snake);
        Q_steps.push(next_steps);
        Q_r.push(nr);
        Q_c.push(nc);
        Q_snake.push(next_snake);
      }
    }
    return res;
  }
  void get_next_snake(std::list<std::pair<int, int> >& current,
                      const int& dir) {
    int& sr = (*current.begin()).first;
    int& sc = (*current.begin()).second;
  }
  bool is_invalid_pos(const int& r, const int& c) {
    if (r < 0 || r >= H || c < 0 || c >= W) return true;
    if (S[r][c] == CHAR_WALL) return true;
    return false;
  }

 private:
};
}  // namespace solution
namespace solution {
class Solution : public SolutionBase {
 public:
 protected:
  virtual bool action() {
    init();
    if (!input()) return false;
    solver.solve();
    output();
    return true;
  }
  bool input() {
    if (!(cin >> H >> W)) return false;
    for (int i = 0; i < H; ++i) {
      cin >> S[i];
    }
    return true;
  }
  void output() {
    if (result == NONE) {
      cout << -1 << endl;
    } else {
      cout << result << endl;
    }
  }

 private:
  Solver solver;
};
}  // namespace solution
int main() { return solution::Solution().run(); }