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.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Collection;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskD {
    int n;
    int m;
    String[] g;
    short[][][] dist;
    int[] pow4;
    final int dx[] = {-1, 0, 1, 0};
    final int dy[] = {0, 1, 0, -1};

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        n = in.nextInt();
        m = in.nextInt();
        g = new String[n];
        for (int i = 0; i < n; i++)
            g[i] = in.next();
        int[] I = new int[9];
        int[] J = new int[9];
        Arrays.fill(I, -1);
        Arrays.fill(J, -1);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) {
                char c = g[i].charAt(j);
                if (c >= '0' && c <= '9') {
                    I[c - '1'] = i;
                    J[c - '1'] = j;
                }
            }
        int k = 0;
        while (k < 9 && I[k] != -1)
            k++;
        pow4 = new int[20];
        pow4[0] = 1;
        for (int i = 1; i < pow4.length; i++)
            pow4[i] = pow4[i - 1] * 4;
        dist = new short[n][m][pow4[k - 1]];
        int initMask = 0;
        for (int i = 0; i < k - 1; i++) {
            int dir = -1;
            for (int j = 0; j < 4; j++)
                if (I[i] + dx[j] == I[i + 1] && J[i] + dy[j] == J[i + 1]) {
                    dir = j;
                    break;
                }
            if (dir == -1)
                throw new RuntimeException();
            initMask += pow4[i] * dir;
        }
        dist[I[0]][J[0]][initMask] = 1;
        Queue<Integer> q = new LinkedList<Integer>();
        q.add(I[0]);
        q.add(J[0]);
        q.add(initMask);
        int[] ci = new int[k];
        int[] cj = new int[k];
        int cleanMask = pow4[k - 1] - 1;
        while (!q.isEmpty()) {
            int i = q.poll();
            int j = q.poll();
            int mask = q.poll();
            int curDist = dist[i][j][mask];
            CC:
            for (int it = 0; it < 4; it++) {
                int i2 = i + dx[it];
                int j2 = j + dy[it];
                if (i2 < 0 || i2 >= n || j2 < 0 || j2 >= m || g[i2].charAt(j2) == '#')
                    continue;
                int newMask = (mask << 2) & cleanMask;
                newMask += (4 + it - 2) & 3;
                ci[0] = i2;
                cj[0] = j2;
                int M = newMask;
                for (int ii = 0; ii < k - 1; ii++) {
                    ci[ii + 1] = ci[ii] + dx[M & 3];
                    cj[ii + 1] = cj[ii] + dy[M & 3];
                    M >>= 2;
                }
                for (int ii = 0; ii < k; ii++)
                    for (int jj = 0; jj < ii; jj++)
                        if (ci[ii] == ci[jj] && cj[ii] == cj[jj])
                            continue CC;
                if (dist[i2][j2][newMask] > 0)
                    continue;
                dist[i2][j2][newMask] = (short) (curDist + 1);
                if (g[i2].charAt(j2) == '@') {
                    out.println(dist[i2][j2][newMask] - 1);
                    return;
                }
                q.add(i2);
                q.add(j2);
                q.add(newMask);
            }
        }
        out.println(-1);
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
}