#include <cstdio>
#include <cstring>
using namespace std;

int map[22][22];
int st[2],gl[2];
int n,m,ans;
int move[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

void dfs(int x,int y,int z){
    if(z>=ans)return;
    for(int i=0;i<4;i++){
        int xx=x,yy=y;
        if(map[x+move[i][0]][y+move[i][1]]!=0&&map[x+move[i][0]][y+move[i][1]]!=3)continue;
        for(int j=0;map[xx][yy]==0;j++){
            xx+=move[i][0];
            yy+=move[i][1];
            if(map[xx][yy]==3){
                ans=z;
                return;
            }
        }
        if(map[xx][yy]==1){
            map[xx][yy]=0;
            dfs(xx-move[i][0],yy-move[i][1],z+1);
            map[xx][yy]=1;
        }
    }
}
int main(){
    scanf("%d%d",&m,&n);
    while(n*m!=0){
        for(int i=0;i<=n+1;i++){
            map[i][0]=-1;
            map[i][m+1]=-1;
        }
        for(int i=0;i<=m+1;i++){
            map[0][i]=-1;
            map[n+1][i]=-1;
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                scanf("%d",&map[i][j]);
                if(map[i][j]==2){
                    st[0]=i;st[1]=j;
                    map[i][j]=0;
                }
            }
        ans=11;
        dfs(st[0],st[1],1);
        if(ans>10)ans=-1;
        printf("%d\n",ans);
        scanf("%d%d",&m,&n);
    }
    
    return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <climits>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector< VI > VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

const LL MOD = 1000000007;

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

const int INF = INT_MAX;

PII S;
PII T;
int W, H;
int ans;

void move(VVI ts, PII p, int n) {
	if (++n > 10) {
		return;
	}

	for (int i = 0; i < 4; i++) {
		int x = p.X;
		int y = p.Y;
		bool first = true;

		while (true) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if (nx < 0 || ny < 0 || nx >= W || ny >= H) {
				break;
			}
			if (first && ts[ny][nx] == 1) {
				break;
			}
			first = false;

			if (ts[ny][nx] == 3) {
				ans = min(ans, n);
				break;
			}

			if (ts[ny][nx] == 1) {
				VVI nts(ts);
				nts[ny][nx] = 0;
				move(nts, make_pair(x, y), n);
				break;
			}

			x = nx;
			y = ny;
		}
	}
}

int main(void) {
	while (cin >> W >> H, W) {
		VVI ts(H, VI(W));
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> ts[i][j];
				if (ts[i][j] == 2) {
					S = make_pair(j, i);
				}
				if (ts[i][j] == 3) {
					T = make_pair(j, i);
				}
			}
		}

		VI pattern(10, 0);
		ans = INF;

		move(ts, S, 0);

		cout << (ans == INF ? -1 : ans) << endl;
	}

	return 0;
}