#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int dx[4]={1, 0, 0,-1};
int dy[4]={0, 1,-1, 0};

int search(int y, int x, int step, vector<vector<int> >& map){
	if(step>10) return -1;
	int ret=1e9;
	for(int i=0; i<4; i++){
		int ny=y+dy[i], nx=x+dx[i];
		if(map[ny][nx]==1) continue;
		while(1){
			if(map[ny][nx]==1){
				map[ny][nx]=0;
				int next = search(ny-dy[i], nx-dx[i], step+1, map);
				if(next!=-1){
					ret = min(next, ret);
				}
				map[ny][nx]=1;
				break;
			}
			if(map[ny][nx]==3){
				return step;
			}
			if(map[ny][nx]==4){
				break;
			}
			ny += dy[i];
			nx += dx[i];
		}
	}
	if(ret == 1e9){
		return -1;
	}
	return ret;
}

int main(){
	while(1){
		int w,h;
		cin >> w >> h;
		if(w==0) break;
		
		vector<vector<int> > map(h+2, vector<int>(w+2, 4));
		int sy, sx;
		for(int i=1; i<=h; i++){
			for(int j=1; j<=w; j++){
				cin >> map[i][j];
				if(map[i][j]==2){
					sy = i;
					sx = j;
				}
			}
		}
		
		cout << search(sy, sx, 1, map) << endl;
	}
	return 0;
}