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.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.concurrent.ArrayBlockingQueue;

public class Solver {

	public static void main(String[] Args) throws NumberFormatException,
			IOException {
		long time=System.currentTimeMillis();
		new Solver().Run();
		if (System.getProperty("ONLINE_JUDGE")==null){
			System.out.println(System.currentTimeMillis()-time);
		}
	}

	PrintWriter pw;
	StringTokenizer Stok;
	BufferedReader br;

	public String nextToken() throws IOException {
		while (Stok == null || !Stok.hasMoreTokens()) {
			Stok = new StringTokenizer(br.readLine());
		}
		return Stok.nextToken();
	}

	public int nextInt() throws NumberFormatException, IOException {
		return Integer.parseInt(nextToken());
	}

	public double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(nextToken());
	}

	public long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(nextToken());
	}

	class TPoint{
		int x;
		int y;
		
		public TPoint(int ax, int ay){
			x=ax;
			y=ay;
		}
	}
	
	char[][] field;
	int wid, hei;
	TPoint start, fin;
	int lenzm;
	int[][][] sits;
	boolean[][] work;
	TPoint[] go;
	
	public int findMask(){
		int result=0;
		int curx=start.x, cury=start.y;
		int curzn=1;
		int k;
		char ch;
		for (; curzn+1<=lenzm; curzn++){
			for (k=0; (int)field[curx+go[k].x][cury+go[k].y]-curzn-(int)'0'!=1; k++);
			result+=(k<<(2*(curzn-1)));
			curx+=go[k].x;
			cury+=go[k].y;
		}
		return result;
	}
	
	public void reading() throws NumberFormatException, IOException{
		hei=nextInt();
		wid=nextInt();
		field=new char[hei+2][wid+2];
		for (int i=0; i<hei+2; i++){
			field[i][0]='#';
			field[i][wid+1]='#';
		}
		for (int j=0; j<wid+2; j++){
			field[0][j]='#';
			field[hei+1][j]='#';
		}
		
		String str;
		for (int i=1; i<=hei; i++){
			str=br.readLine();
			for (int j=1; j<=wid; j++){
				field[i][j]=str.charAt(j-1);
				if (field[i][j]=='@'){
					fin=new TPoint(i, j);
				}
				else
					if (field[i][j]=='1'){
						start=new TPoint(i, j);
					}
					else
						if (field[i][j]>='1' && field[i][j]<='9'){
							lenzm=Math.max(lenzm, (int)field[i][j]-(int)'1');
						}
			}
		}
		
		sits=new int[hei+2][wid+2][1<<(2*(lenzm-1))];
		int infin=(int) 1e9;
		for (int i=0; i<=hei; i++){
			for (int j=0; j<=wid; j++){
				Arrays.fill(sits[i][j],infin);
			}
		}
		
		work=new boolean[20][20];
		go=new TPoint[4];
		go[0]=new TPoint(0, 1);
		go[1]=new TPoint(1, 0);
		go[2]=new TPoint(0, -1);
		go[3]=new TPoint(-1, 0);
	}
	
	public void drawSn(int x, int y, int ms){
		for (int k=0; k<4; k++){
			work[10+go[k].x][10+go[k].y]=false;
			if (field[x+go[k].x][y+go[k].y]=='#')
				work[10+go[k].x][10+go[k].y]=false;
			else
				work[10+go[k].x][10+go[k].y]=true;
		}
		
		int curx=10;
		int cury=10;
		int curk;
		for (int i=0; i<lenzm-1; i++){
			curk=ms&3;
			curx+=go[curk].x;
			cury+=go[curk].y;
			work[curx][cury]=false;
			ms=ms>>2;
		}
	}
	
	public void solve() throws NumberFormatException, IOException{
		reading();
		
		Queue<int[]> que=new ArrayDeque<int[]>();
		//Queue<int[]> que=new ArrayBlockingQueue<int[]>(15*15*17000);
		//Queue<int[]> que=new LinkedList<int[]>();
		int zn=findMask();
		sits[start.x][start.y][zn]=0;
		que.add(new int[]{start.x, start.y, zn});
		while (!que.isEmpty()){
			int[] cur=que.poll();
			int curx=cur[0];
			int cury=cur[1];
			int curms=cur[2];
			int newms;
			int newx, newy;
			drawSn(curx, cury, curms);
			for (int k=0; k<4; k++){
				if (work[10+go[k].x][10+go[k].y]){
					newms=(curms<<2)&((1<<(2*(lenzm-1)))-1)|(k^2);
					newx=curx+go[k].x;
					newy=cury+go[k].y;
					if (sits[newx][newy][newms]>sits[curx][cury][curms]+1){
						sits[newx][newy][newms]=sits[curx][cury][curms]+1;
						if (fin.x==newx && fin.y==newy){
							pw.println(sits[newx][newy][newms]);
							return;
						}
						que.add(new int[]{newx, newy, newms});
					}
				}
			}
		}
		pw.println(-1);
	}
	
	public void Run() throws NumberFormatException, IOException {
		//br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter("output.txt");
		br=new BufferedReader(new InputStreamReader(System.in));  pw=new PrintWriter(new OutputStreamWriter(System.out));
		solve();
		pw.flush();
		pw.close();
	}

}