#include<iostream>
using namespace std;

int w,h,gx,gy;

//左下上右
int dx[4] = {-1,0,0,1};
int dy[4] = {0,1,-1,0};

int solve(int count,int stage[20][20],int px,int py){
	int ans = 11;
	if(count == 10){
		return -1;
	}
	

	//左側
	for(int i=0;i<4;i++){
		bool flag = false;
		int bufx = px;
		int bufy = py;
		int c = 0;
		while(true){
			c++;
			bufx += dx[i];
			bufy += dy[i];

			if(bufx < 0 || bufx >= w || bufy < 0 || bufy >= h){
				break;
			}
			if(stage[bufy][bufx] == 1){
				if(c != 1){
					flag = true;
				}
				break;
			}
			//ゴールならカウント足して返す
			if(bufy == gy && bufx == gx){
				return count+1;
			}
		}
		//スライド可能なら
		if(flag){
			stage[bufy][bufx] = 0;
			int d = solve(count+1,stage,bufx+(dx[i]*-1),bufy+(dy[i]*-1));
			if(d != -1){
				ans = min(ans,d);
			}
			stage[bufy][bufx] = 1;
		}
	}

	if(ans == 11){
		return -1;
	}
	return ans;
}

int main(){
	int px,py;
	while(true){
		int stage[20][20];
		cin >> w >> h;
		if(w == 0 && h == 0){
			break;
		}

		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin >> stage[i][j];
				if(stage[i][j] == 2){
					px = j;
					py = i;
				}
				if(stage[i][j] == 3){
					gx = j;
					gy = i;
				}
			}
		}

		cout << solve(0,stage,px,py) << endl;
	}
}