#include <iostream>
#include <vector>

using namespace std;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};

int ans = 11;
vector<vector<int>> mp;
void dfs(int i, int j, int direction, int num, vector<vector<int>> mp){
	while(mp[i+dx[direction]][j+dy[direction]] == 0){
		i += dx[direction];
		j += dy[direction];
	}
	if(mp[i+dx[direction]][j+dy[direction]] == 3){
		ans = min(num,ans);
		return;
	}
	if(mp[i+dx[direction]][j+dy[direction]] == 1 && num < 10){
		mp[i+dx[direction]][j+dy[direction]] = 0;
		for(int k = 0;k<4;k++){
			if(mp[i+dx[k]][j+dy[k]] == 1)continue;
			if(mp[i+dx[k]][j+dy[k]] == -1)continue;
			dfs(i,j,k,num+1,mp);
		}
	}
}


int main(){
	int w,h;
	while(cin>>w>>h,w){
		mp.resize(h+2,vector<int>(w+2,-1));
		int def_i,def_j;
		for(int i = 1;i < h+1;i++){
			for(int j = 1;j < w+1;j++){
				cin >> mp[i][j];
				if(mp[i][j] == 2){
					def_i = i;
					def_j = j;
				}
			}
		}
		mp[def_i][def_j] = 0;
		for(int i = 0;i < 4;i++){
			if(mp[def_i+dx[i]][def_j+dy[i]] != 0 && mp[def_i+dx[i]][def_j+dy[i]] != 3)continue;
			dfs(def_i,def_j,i,1,mp);
		}
		if(ans == 11)cout << -1 << endl;
		else cout << ans << endl;
		ans = 11;
		mp.clear();
	}
}