#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cstring>

using namespace std;

struct State {
    int r, c, dr, dc, chk;
};

int dp[45][45][19][19][11];

void solve() {
    int w, h_val;
    while (cin >> w >> h_val && (w != 0 || h_val != 0)) {
        vector<string> grid(h_val);
        cin.ignore();
        for (int i = 0; i < h_val; i++) {
            getline(cin, grid[i]);
            if (!grid[i].empty() && grid[i].back() == '\r') {
                grid[i].pop_back();
            }
            if ((int)grid[i].length() < w) {
                grid[i].append(w - grid[i].length(), ' ');
            }
        }
        
        int start_r = -1, start_c = -1;
        int end_val = 0;
        for (int i = 0; i < h_val; i++) {
            for (int j = 0; j < w; j++) {
                if (grid[i][j] == '0') {
                    start_r = i;
                    start_c = j;
                }
                if (grid[i][j] <= '9' && grid[i][j] > '0') {
                    end_val = max(end_val, grid[i][j] - '0');
                }
            }
        }
        
        if (start_r == -1 || start_c == -1) {
            cout << -1 << "\n";
            continue;
        }
        
        memset(dp, -1, sizeof(dp));
        dp[start_r][start_c][9][9][0] = 0;
        
        queue<State> q;
        q.push({start_r, start_c, 0, 0, 0});
        
        bool found = false;
        while (!q.empty()) {
            State on = q.front();
            q.pop();
            
            int offset = 0;
            if (grid[on.r][on.c] <= '9' && grid[on.r][on.c] > '0') {
                int val = grid[on.r][on.c] - '0';
                if (val == end_val && on.chk == end_val - 1) {
                    cout << dp[on.r][on.c][on.dr + 9][on.dc + 9][on.chk] << "\n";
                    found = true;
                    break;
                }
                if (val == on.chk + 1) {
                    on.chk++;
                    offset = 1;
                }
            }
            
            for (int i = -1; i <= 1; i++) {
                for (int j = -1; j <= 1; j++) {
                    int ndr = on.dr + i;
                    int ndc = on.dc + j;
                    int nr = on.r + ndr;
                    int nc = on.c + ndc;
                    
                    // INCORRECT LIMIT: restrict velocity to [-7, 7] instead of [-9, 9]
                    if (ndr >= -7 && ndr <= 7 && ndc >= -7 && ndc <= 7) {
                        if (nr >= 0 && nr < h_val && nc >= 0 && nc < w) {
                            if (grid[nr][nc] <= '9') {
                                if (dp[nr][nc][ndr + 9][ndc + 9][on.chk] == -1) {
                                    dp[nr][nc][ndr + 9][ndc + 9][on.chk] = dp[on.r][on.c][on.dr + 9][on.dc + 9][on.chk - offset] + 1;
                                    q.push({nr, nc, ndr, ndc, on.chk});
                                }
                            }
                        }
                    }
                }
            }
        }
        if (!found) {
            cout << -1 << "\n";
        }
    }
}

int main() {
    solve();
    return 0;
}
