#include<iostream>
using namespace std;

int field[21][21];
int w, h, res;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

void dfs(int x, int y, int n) {
    if (n >= res) return;
    for (int i = 0; i < 4; i++) {
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];
        
        if (tx < 0 || tx >= w || ty < 0 || ty >= h) continue;
        if (field[ty][tx] == 1) continue;
        if (field[ty][tx] == 3) { res = n; return; }
        
        while (true) {
            tx += dir[i][0];
            ty += dir[i][1];
            if (tx < 0 || tx >= w || ty < 0 || ty >= h) break;
            if (field[ty][tx] == 3) { res = n; return; }
            if (field[ty][tx] == 1) {
                field[ty][tx] = 0;
                dfs(tx - dir[i][0], ty - dir[i][1], n + 1);
                field[ty][tx] = 1;
                break;
            }
        }
    }
}

int main()
{
    int x, y;
    while (cin >> w >> h, w | h) {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> field[i][j];
                if (field[i][j] == 2) {
                    x = j;
                    y = i;
                }
            }
        }
        res = 11;
        dfs(x ,y, 1);
        cout << (res > 10 ? -1 : res) << endl;
    }
    
    return 0;
}