#include<iostream>
#include<set>
#include<string>
#include<queue>
#include<string.h>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
#include<stack>
#include<sstream>
using namespace std;
int m, n;
struct point {
	int x, y;
	int status;//状态，运动或静止
};
point s, goal;
int MinStep;
int board[22][22];
void dfs(int i,int j,bool status,bool flag,int direction,int step) {
	//direction 为当前方向 0: east;1: west;2: south; 3: north
	//flag为是否消除block 0: 消除
	//status 当前状态 0: 静止
	if (step > 10) {//
		return;
	}
	if (board[i][j] == 3) {
		if (MinStep > step) {
			MinStep = step;
			return;
		}
	}
	if (!flag) {
		switch (direction)
		{
		case 0:
			board[i][j + 1] = 0;
			break;
		case 1:
			board[i][j - 1] = 0;
			break;
		case 2:
			board[i + 1][j] = 0;
			break;
		case 3:
			board[i - 1][j] = 0;
			break;
		}
	}
	if (!status) {//静止，选择方向
		if (j + 1 < m && (board[i][j + 1] == 0 || board[i][j + 1] == 3))//east
			dfs(i, j + 1, true, true, 0,step+1);
		if (j - 1 >= 0 && (board[i][j - 1] == 0 || board[i][j - 1] == 3))//west
			dfs(i, j - 1, true, true, 1, step + 1);
		if (i + 1 < n && (board[i + 1][j] == 0 || board[i + 1][j] == 3))//south
			dfs(i + 1, j, true, true, 2, step + 1);
		if (i - 1 >= 0 && (board[i - 1][j] == 0 || board[i - 1][j] == 3))//north
			dfs(i - 1, j, true, true, 3, step + 1);
	}
	else if (status) {//运动
		switch (direction)
		{
		case 0://east
			if (j + 1 > m - 1)//越界
				return;
			if (board[i][j + 1] == 0)//下一位置为0，继续运动
				dfs(i, j + 1, true, true, 0, step);
			if (board[i][j + 1] == 1)//下一位置为1，停止运动，消除石头
				dfs(i, j, false, false, 0, step);
			if (board[i][j + 1] == 3)//下一位置为终点，停止运动，游戏结束
				dfs(i, j + 1, false, true, 0, step);
			break;
		case 1://west
			if (j - 1 < 0)
				return;
			if (board[i][j - 1] == 0)
				dfs(i, j - 1, true, true, 1, step);
			if (board[i][j - 1] == 1)
				dfs(i, j, false, false, 1, step);
			if (board[i][j - 1] == 3)
				dfs(i, j - 1, false, true, 1, step);
			break;
		case 2://south
			if (i + 1 > n - 1)
				return;
			if (board[i + 1][j] == 0)
				dfs(i + 1, j, true, true, 2, step);
			if (board[i + 1][j] == 1)
				dfs(i, j, false, false, 2, step);
			if (board[i + 1][j] == 3)
				dfs(i + 1, j, false, true, 2, step);
			break;
		case 3://north
			if (i - 1 < 0)
				return;
			if (board[i - 1][j] == 0)
				dfs(i - 1, j, true, true, 3, step);
			if (board[i - 1][j] == 1)
				dfs(i, j, false, false, 3, step);
			if (board[i - 1][j] == 3)
				dfs(i - 1, j, false, true, 3, step);
			break;
		}
	}
	if (!flag) {//恢复消除的石头
		switch (direction)
		{
		case 0:
			board[i][j + 1] = 1;
			break;
		case 1:
			board[i][j - 1] = 1;
			break;
		case 2:
			board[i + 1][j] = 1;
			break;
		case 3:
			board[i - 1][j] = 1;
			break;
		}
	}
	return;
}
int main() {
	while (cin >> m >> n && (m + n)) {
		for(int i=0;i<n;i++)
			for (int j = 0; j < m; j++)
			{
				cin >> board[i][j];
				if (board[i][j] == 2) {//起点
					s.x = i;
					s.y = j;
					s.status = false;
					board[i][j] = 0;
				}
				if (board[i][j] == 3) {//终点
					goal.x = i;
					goal.y = j;
				}
			}
		MinStep = 20;//将最小步数初始设置为较大值
		dfs(s.x, s.y, s.status, true, 0, 0);
		if (MinStep > 10)
			cout << "-1" << endl;
		else
			cout << MinStep << endl;
	}
	return 0;
}


Measured memory_bytes: 3016