#include <bits/stdc++.h>
using namespace std;

#define rep(i,j) for (int i=0;i<j;++i)

int w, h, cnt = -1;
int filed[21][21];

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

void bfs(int y, int x, int t, int d) {
    if (t > 10) return;
    int nx = x+dx[d], ny = y+dy[d];
    while (0 <= nx && nx < w && 0 <= ny && ny < h && filed[ny][nx] == 0) { nx += dx[d]; ny += dy[d]; }
    if (filed[ny][nx] == 3) {
        if (cnt == -1) cnt = t;
        else cnt = min(cnt, t);
        return;
    }
    if (nx == x+dx[d] && ny == y+dy[d]) return;
    else if (0 <= nx && nx < w && 0 <= ny && ny < h) {
        if (filed[ny][nx] == 1) {
            filed[ny][nx] = 0;
            for (int i = 0; i < 4; ++i) {
                bfs(ny-dy[d], nx-dx[d], t+1, i);
            }
            filed[ny][nx] = 1;
        }
    }
    return;
}

int main() {
    while (cin >> w >> h, (w||h)) {
        rep(i, 21) rep(j, 21) filed[i][j] = 0;
        int py, px;
        rep(y, h) rep(x, w) {
            cin >> filed[y][x];
            if (filed[y][x] == 2) {
                py = y; px = x;
                filed[y][x] = 0;
            }
        }
        cnt = -1;
        for (int i = 0; i < 4; ++i) {
            bfs(py, px, 1, i);
        }
        cout << cnt << endl;
    }
}