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 JAVA):

Solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

public class Main {
	public static BufferedReader in;
	public static PrintWriter out;

	public static void main(String[] args) throws IOException {

		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);

		boolean showLineError = true;
		if (showLineError) {
			solve();
			out.close();
		} else {
			try {
				solve();
			} catch (Exception e) {
			} finally {
				out.close();
			}
		}

	}

	static void debug(Object... os) {
		out.println(Arrays.deepToString(os));
	}

	private static int[] dr = { 1, 0, -1, 0 };
	private static int[] dc = { 0, 1, 0, -1 };
	private static boolean[][] initial;
	private static SnakeQueue q;

	private static void solve() throws IOException {
		String[] line = in.readLine().split(" ");
		int n = Integer.parseInt(line[0]);
		int m = Integer.parseInt(line[1]);
		initial = new boolean[n][m];
		int[][] snake = new int[9][]; // en cero esta la cabeza
		int[] apple = new int[2];
		for (int i = 0; i < n; i++) {
			String s = in.readLine();
			for (int j = 0; j < m; j++) {
				initial[i][j] = s.charAt(j) == '#';
				if (s.charAt(j) == '@')
					apple = new int[] { i, j };
				if (Character.isDigit(s.charAt(j)))
					snake[s.charAt(j) - '1'] = new int[] { i, j };
			}
		}
		Snake start = new Snake(snake);
		int k = Snake.size;
		int[][] write = new int[k][2];
		Map<Integer,Integer>[][] bfs = (Map<Integer,Integer>[][]) new Map[n][m];
		for(int i =0;i<n;i++)
			for(int j =0;j<m;j++)
				bfs[i][j]= new TreeMap<Integer,Integer>();
		bfs[start.tail_r][start.tail_c].put(start.mask, 0) ;
		q = new SnakeQueue();
		q.add(start);
		Snake aux = new Snake(0, 0, 0);
		while (!q.isEmpty()) {
			int s = q.poll();
			Snake.coordinates(write, s);
			for (int i = 0; i < write.length - 1; i++) {
				int[] coor = write[i]; // tail no va
				initial[coor[0]][coor[1]] = true;
			}
			int[] head = write[0];
			for (int p = 0; p < 4; p++) {
				int r = head[0] + dr[p];
				int c = head[1] + dc[p];
				if (inside(r, n) && inside(c, m) && !initial[r][c]) {
					Snake.move(aux, p, s);
					if (apple[0] == r && apple[1] == c) {
						out.println(1 + bfs[q.tails_r[s]][q.tails_c[s]].get(q.masks[s]));
						return;
					} else if (!bfs[aux.tail_r][aux.tail_c].containsKey(aux.mask)) {
						bfs[aux.tail_r][aux.tail_c].put(aux.mask, 1+bfs[q.tails_r[s]][q.tails_c[s]].get(q.masks[s]));
						q.add(aux);
					}
				}
			}
			for (int i = 0; i < write.length - 1; i++) {
				int[] coor = write[i]; // tail no va
				initial[coor[0]][coor[1]] = false;
			}

		}
		out.println(-1);

	}

	private static boolean inside(int k, int n) {
		return 0 <= k && k < n;
	}

	private static class SnakeQueue {
		int lo;
		int hi;

		int[] masks;
		short[] tails_r;
		short[] tails_c;

		public SnakeQueue() {
			masks = new int[10000000];
			tails_r = new short[10000000];
			tails_c = new short[10000000];
			lo = hi = 0;

		}

		public int poll() {
			return lo++;
		}

		public boolean isEmpty() {
			return lo == hi;
		}

		public void add(Snake s) {
			tails_r[hi] = (short) s.tail_r;
			tails_c[hi] = (short) s.tail_c;
			masks[hi] = s.mask;
			hi++;
		}

	}

	private static class Snake {
		public static int size;

		private static int encodedDir(int r, int c) {
			if (r == 1 && c == 0)
				return 0;
			if (r == 0 && c == 1)
				return 1;
			if (r == -1 && c == 0)
				return 2;
			if (r == 0 && c == -1)
				return 3;
			return -1;

		}

		public int tail_r;
		public int tail_c;

		public int mask;

		public Snake(int tail_r, int tail_c, int mask) {
			this.tail_r = tail_r;
			this.tail_c = tail_c;
			this.mask = mask;
		}

		public Snake(int[][] snake) {
			size = 0;
			for (size = 0; size < 9; size++)
				if (snake[size] == null)
					break;
			tail_r = snake[size - 1][0];
			tail_c = snake[size - 1][1];

			int base = 1;
			for (int i = size - 1; i >= 1; i--) {
				int r = snake[i - 1][0] - snake[i][0];
				int c = snake[i - 1][1] - snake[i][1];
				int code = encodedDir(r, c);
				mask += code * base;
				base *= 4;
			}

		}

		public static void move(Snake ret,int dir, int index) {
			int last_move = q.masks[index] % 4;
			int new_tail_r = q.tails_r[index] + dr[last_move];
			int new_tail_c = q.tails_c[index] + dc[last_move];
			int newMask = q.masks[index] / 4;
			newMask += (1 << (2 * (size - 2))) * dir;
			ret.mask=newMask;
			ret.tail_c=new_tail_c;
			ret.tail_r=new_tail_r;
		}

		public static void coordinates(int[][] ret, int index) {
			int aux = q.masks[index];
			ret[size - 1][0] = q.tails_r[index];
			ret[size - 1][1] = q.tails_c[index];

			for (int i = size - 2; i >= 0; i--) {
				int a = aux % 4;
				aux /= 4;
				int r = ret[i + 1][0] + dr[a];
				int c = ret[i + 1][1] + dc[a];

				ret[i][0] = r;
				ret[i][1] = c;

			}
		}
	}

	private static String[] nss() throws IOException {
		return in.readLine().split(" ");
	}

	private static int ni() throws NumberFormatException, IOException {
		return Integer.parseInt(in.readLine());
	}
}