#include <bits/stdc++.h>
using namespace std;
char Map[20][20];
int n, m;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int op[] = {2, 3, 0, 1};
int len;
struct node {
  long long num;
  int step;
  node() {}
};
queue<node> q;
int x[20], y[20];
node Hash() {
  len = 0;
  int nowx, nowy;
  node no;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if ((Map[i][j] >= '1' && Map[i][j] <= '9')) {
        int num = Map[i][j] - '1' + 1;
        x[num] = i;
        y[num] = j;
        len = max(len, num);
      } else if (Map[i][j] >= 'a' && Map[i][j] <= 'f') {
        int num = Map[i][j] - 'a' + 10;
        x[num] = i;
        y[num] = j;
        len = max(len, num);
      }
    }
  }
  long long num = 0;
  nowx = x[1];
  nowy = y[1];
  for (int i = 2; i <= len; i++) {
    for (int j = 0; j < 4; j++) {
      if (nowx + dx[j] == x[i] && nowy + dy[j] == y[i]) {
        num |= j << ((i - 2) * 2);
        nowx = x[i];
        nowy = y[i];
        break;
      }
    }
  }
  num <<= 4;
  num |= x[1];
  num <<= 4;
  num |= y[1];
  no.num = num;
  return no;
}
void deHash(node no) {
  long long num = no.num;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if ((Map[i][j] >= '1' && Map[i][j] <= '9') ||
          (Map[i][j] >= 'a' && Map[i][j] <= 'f')) {
        Map[i][j] = '.';
      }
    }
  }
  int nowy = num & 15;
  num >>= 4;
  int nowx = num & 15;
  num >>= 4;
  Map[nowx][nowy] = '1';
  for (int i = 1; i < len; i++) {
    int to = num & 3;
    num >>= 2;
    nowx += dx[to];
    nowy += dy[to];
    Map[nowx][nowy] = i > 8 ? i - 9 + 'a' : i + '1';
  }
}
void Init() {
  memset(Map, 0, sizeof(Map));
  for (int i = 0; i < n; i++) {
    scanf("%s", Map[i]);
  }
}
void test() {
  for (int i = 0; i < n; i++) {
    printf("%s\n", Map[i]);
  }
}
map<long long, bool> vis;
int bfs() {
  vis.clear();
  while (!q.empty()) q.pop();
  node no = Hash();
  vis[no.num] = 1;
  no.step = 0;
  q.push(no);
  while (!q.empty()) {
    node now = q.front();
    q.pop();
    deHash(now);
    for (int i = 0; i < 4; i++) {
      node tmp = now;
      long long num = tmp.num;
      int nowy = num & 15;
      num >>= 4;
      int nowx = num & 15;
      num >>= 4;
      if (nowx + dx[i] < 0 || nowx + dx[i] >= n || nowy + dy[i] < 0 ||
          nowy + dy[i] >= m)
        continue;
      if (Map[nowx + dx[i]][nowy + dy[i]] == '.' ||
          Map[nowx + dx[i]][nowy + dy[i]] ==
              (len > 9 ? len - 10 + 'a' : '1' + len - 1)) {
        num <<= 2;
        num &= (1 << ((len - 1) * 2)) - 1;
        num |= op[i];
        num <<= 4;
        num |= nowx + dx[i];
        num <<= 4;
        num |= nowy + dy[i];
        if (vis[num] != 1) {
          tmp.step = tmp.step + 1;
          tmp.num = num;
          vis[num] = 1;
          q.push(tmp);
        }
      } else if (Map[nowx + dx[i]][nowy + dy[i]] == '@') {
        return now.step + 1;
      }
    }
  }
  return -1;
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    Init();
    printf("%d\n", bfs());
  }
  return 0;
}
