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.*;

public class D implements Runnable {
	static BufferedReader in;
	static PrintWriter out;
	static StringTokenizer st;
	static Random rnd;

	final int inf = Integer.MAX_VALUE;
	final int[] dx = { 1, -1, 0, 0 };
	final int[] dy = { 0, 0, 1, -1 };
	final long p = 43;
	final int maxField = 2000;
	char[][][] poll;
	int[] qx, qy;
	int[][] fast, slow;
	ArrayList<Integer> free;

	class Field {
		char[][] f;
		int id;
		long hashCode;

		public Field(char[][] f, int id) {
			this.f = f;
			this.id = id;

			hashCode = 0;
			long mod = 1;
			for (int i = 0; i < f.length; i++) {
				for (int j = 0; j < f[i].length; j++) {
					mod *= p;

					if (Character.isDigit(f[i][j])) {
						long code = f[i][j] - '0';
						hashCode += code * mod;
					}
				}
			}
		}

	}

	long its = 0;

	void solve() throws IOException {
		int n = nextInt(), m = nextInt();

		allocPoll(n, m);

		int startId = pollId();
		char[][] field = poll[startId];
		char max = '0';

		int ex = -1, ey = -1;

		for (int i = 0; i < n; i++) {
			String s = nextToken();
			for (int j = 0; j < m; j++) {
				field[i][j] = s.charAt(j);

				if (Character.isDigit(field[i][j]))
					++max;

				if (field[i][j] == '@') {
					ex = i;
					ey = j;
				}
			}
		}

		int res = inf, it = 0;

		Queue<Field> q = new ArrayDeque<Field>();
		HashMap<Long, Integer> map = new HashMap<Long, Integer>();
		Field startField = new Field(field, startId);
		q.add(startField);
		map.put(startField.hashCode, 0);

		bfs: while (q.size() > 0 && it < 1000) {
			++it;
			Field cur = q.poll();
			int d = map.get(cur.hashCode) + 1;

			int hx = -1, hy = -1;

			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					if (cur.f[i][j] == '1') {
						hx = i;
						hy = j;
					}
				}
			}

			int tryFast = getTryFast(n, m, cur.f, hx, hy, ex, ey, max);

			if (tryFast != -1) {
				res = Math.min(res, d + tryFast - 1);
			}

			if (fast[ex][ey] != -1) {

				for (int k = 0; k < 4; k++) {
					int nhx = hx + dx[k], nhy = hy + dy[k];

					if (nhx >= 0 && nhy >= 0 && nhx < n && nhy < m) {
						if (field[nhx][nhy] == '@') {
							res = d;
							break bfs;
						} else if (field[nhx][nhy] == '#'
								|| (Character.isDigit(cur.f[nhx][nhy]) && cur.f[nhx][nhy] != max)) {
							continue;
						} else {
							int fieldId = move(n, m, cur.f, max);
							char[][] newField = poll[fieldId];
							newField[nhx][nhy] = '1';
							Field newState = new Field(newField, fieldId);
							if (!map.containsKey(newState.hashCode)) {
								map.put(newState.hashCode, d);
								q.add(newState);
							} else {
								addId(fieldId);
							}
						}
					}
				}
			}

			addId(cur.id);

		}

		if (res >= inf)
			res = -1;

		out.println(res);
	}

	private int getTryFast(int n, int m, char[][] f, int sx, int sy, int ex,
			int ey, char max) {
		for (int i = 0; i < n; i++) {
			Arrays.fill(fast[i], -1);
			Arrays.fill(slow[i], -1);
		}

		{
			int qs = 0, qe = 0;

			fast[sx][sy] = 0;
			qx[qe] = sx;
			qy[qe] = sy;
			++qe;

			while (qs < qe) {
				int x = qx[qs], y = qy[qs];
				++qs;
				int d = fast[x][y] + 1;

				for (int k = 0; k < 4; k++) {
					int nx = x + dx[k], ny = y + dy[k];

					if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
						if (f[nx][ny] == '#') {
							continue;
						} else {
							if (fast[nx][ny] == -1) {
								fast[nx][ny] = d;
								qx[qe] = nx;
								qy[qe] = ny;
								++qe;
							}
						}
					}
				}
			}

		}

		{
			int qs = 0, qe = 0;

			slow[sx][sy] = 0;
			qx[qe] = sx;
			qy[qe] = sy;
			++qe;

			while (qs < qe) {
				int x = qx[qs], y = qy[qs];
				++qs;
				int d = slow[x][y] + 1;

				for (int k = 0; k < 4; k++) {
					int nx = x + dx[k], ny = y + dy[k];

					if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
						if (f[nx][ny] == '#'
								|| (Character.isDigit(f[nx][ny]) && f[nx][ny] != max)) {
							continue;
						} else {
							if (slow[nx][ny] == -1) {
								slow[nx][ny] = d;
								qx[qe] = nx;
								qy[qe] = ny;
								++qe;
							}
						}
					}
				}
			}

		}

		if (fast[ex][ey] == slow[ex][ey])
			return slow[ex][ey];

		return -1;
	}

	private int pollId() {
		int res = free.get(free.size() - 1);
		free.remove(free.size() - 1);
		return res;
	}

	private void addId(int id) {
		free.add(id);
	}

	private void allocPoll(int n, int m) {
		poll = new char[maxField][n][m];

		free = new ArrayList<Integer>();

		for (int i = 0; i < maxField; i++)
			free.add(i);

		qx = new int[n * m];
		qy = new int[n * m];
		fast = new int[n][m];
		slow = new int[n][m];
	}

	private int move(int n, int m, char[][] f, char max) {
		int resId = pollId();
		char[][] res = poll[resId];

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				++its;
				res[i][j] = f[i][j];

				if (res[i][j] == max) {
					res[i][j] = '.';
				} else if (Character.isDigit(res[i][j])) {
					++res[i][j];
				}
			}
		}

		return resId;
	}

	public static void main(String[] args) {
		final boolean oldChecker = false;

		if (oldChecker)
			new Thread(null, new D(), "yarrr", 1 << 24).start();
		else
			new D().run();
	}

	public void run() {
		try {
			final String className = this.getClass().getName().toLowerCase();

			try {
				in = new BufferedReader(new FileReader(className + ".in"));
				out = new PrintWriter(new FileWriter(className + ".out"));
			} catch (FileNotFoundException e) {
				in = new BufferedReader(new InputStreamReader(System.in));
				out = new PrintWriter(System.out);
			}

			rnd = new Random();

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(42);
		}
	}

	String nextToken() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			String line = in.readLine();

			if (line == null)
				return null;

			st = new StringTokenizer(line);
		}

		return st.nextToken();
	}

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

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

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