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.*; 
import java.util.*;
import java.math.*;
import java.awt.geom.*;

import static java.lang.Math.*;

public class Solution implements Runnable {
	
	int dx [] = {0, 0, 1, -1};
	int dy [] = {-1, 1, 0, 0};
	
	int len = 0;
	int n, m;
	char s[][];
	
	int getLen () {
		return len * 2 - 2;
	}
	
	int pushUp (int mask) {
		return ((mask << 2) | 0) & ((1 << getLen()) - 1);
	}
	
	int pushDown (int mask) {
		return ((mask << 2) | 1) & ((1 << getLen()) - 1);
	}
	
	int pushRight (int mask) {
		return ((mask << 2) | 2) & ((1 << getLen()) - 1);
	}

	int pushLeft (int mask) {
		return ((mask << 2) | 3) & ((1 << getLen()) - 1);
	}
	
	int getValue (int mask, int k) {
		return (mask >> (k * 2)) & ((1 << 2) - 1);
	}
	
	boolean isValid (int i, int j) {
		return i >= 0 && j >= 0 && i < n && j < m;
	}
	
	boolean checkRoute (int i, int j, int ti, int tj, int mask) {
		if (!isValid(i, j)) return false;
		if (s[i][j] == '#') return false;
		
		for (int k = 0; k < getLen() / 2; k++) {
			if (i == ti && j == tj) return false;
			int dir = getValue(mask, k);
			if (dir == 0) ti++;
			if (dir == 1) ti--;
			if (dir == 2) tj--;
			if (dir == 3) tj++;
		}
		
		return true;
	}
	
	int getMask (int state) {
		return state & ((1 << 18) - 1);
	}
	
	int getJ (int state) {
		return (state >> 18) & ((1 << 5) - 1);
	}
	
	int getI (int state) {
		return (state >> 23) & ((1 << 5) - 1);
	}
	
	int toState (int i, int j, int mask) {
		return (i << 23) | (j << 18) | mask;
	}
	
	public void solve () throws Exception {
		n = nextInt();
		m = nextInt();
		s = new char [n][m];
		for (int i = 0; i < n; i++) {
			s [i] = in.readLine().toCharArray();
		}
		
		int si = -1, sj = -1;
		int ti = -1, tj = -1;

		int fi = -1, fj = -1;
		
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (s[i][j] >= '1' && s[i][j] <= '9') {
					if (s[i][j] - '0' > len) {
						len = s[i][j] - '0';
						ti = i;
						tj = j;
					}
				}
				if (s[i][j] == '1') {
					si = i;
					sj = j;
				}
				if (s[i][j] == '@') {
					fi = i;
					fj = j;
				}
			}
		}
		
		int d [][][] = new int [n][m][(1 << getLen())];
		
		int mask = 0;
		for (int i = len; i >= 2; i--) {
			if (isValid (ti - 1, tj) && s[ti - 1][tj] == (char)(i - 1) + '0') {
				mask = pushUp(mask);
				ti--;
			} else if (isValid (ti + 1, tj) && s[ti + 1][tj] == (char)(i - 1) + '0') {
				mask = pushDown(mask);
				ti++;
			} else if (isValid (ti, tj + 1) && s[ti][tj + 1] == (char)(i - 1) + '0') {
				mask = pushRight(mask);
				tj++;
			} else if (isValid (ti, tj - 1) && s[ti][tj - 1] == (char)(i - 1) + '0') {
				mask = pushLeft(mask);
				tj--;
			}
		}
		
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				Arrays.fill(d[i][j], Integer.MAX_VALUE / 2);
			}
		}
		
		d[si][sj][mask] = 0;
		LinkedList<Integer> q = new LinkedList<>();
		q.add(toState(si, sj, mask));
		
		while (!q.isEmpty()) {
			int state = q.pollFirst();
			int cMask = getMask(state);
			int i = getI(state);
			int j = getJ(state);
			for (int k = 0; k < 4; k++) {
				int ni = dy[k] + i;
				int nj = dx[k] + j;
				int nMask = 0;
				if (k == 0) nMask = pushUp(cMask);
				if (k == 1) nMask = pushDown(cMask);
				if (k == 2) nMask = pushRight(cMask);
				if (k == 3) nMask = pushLeft(cMask);
				if (checkRoute(ni, nj, i, j, cMask) && d[ni][nj][nMask] > d[i][j][cMask] + 1) {
					d[ni][nj][nMask] = d[i][j][cMask] + 1;
					q.addLast(toState(ni, nj, nMask));
				}
			}
		}
		
		int answer = Integer.MAX_VALUE / 2;
		
		for (mask = 0; mask < (1 << getLen()); mask++) {
			answer = min (answer, d[fi][fj][mask]);
		}
		
		if (answer == Integer.MAX_VALUE / 2) answer = -1;
		
		out.println(answer);
	}
	
	private static long stime = 0;
	
	private BufferedReader in;
	private PrintWriter out;
	private StringTokenizer st;
	
	private String nextToken () throws Exception {
		while (st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(in.readLine());
		}
		return st.nextToken();
	}
	
	private int nextInt () throws Exception {
		return Integer.parseInt(nextToken());
	}
	
	private long nextLong () throws Exception {
		return Long.parseLong(nextToken());
	}
	
	private double nextDouble () throws Exception {
		return Double.parseDouble(nextToken());
	}
	
	@Override
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(new OutputStreamWriter(System.out));
			
			solve ();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			out.close();
		}
	}
	
	public static void main(String[] args) {
		new Thread(null, new Solution(), "", 1<<26).start();
	}

}