#include<stdio.h>
#include<string.h>
int Min(int x,int y){return x<y?x:y;}
int m[22][22],h,w;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int F(int n,int ax,int ay)
{
	if(n>9)return 99;
	int res=99,i,x,y;
	for(i=0;i<4;++i)
	{
		x=ax+dx[i],y=ay+dy[i];
		if(m[y][x]==1)continue;
		for(;1<=x&&x<=w&&1<=y&&y<=h;x+=dx[i],y+=dy[i])
		{
			if(m[y][x]==3)return 1;
			if(m[y][x]==1)
			{
				m[y][x]=0;
				res=Min(res,F(n+1,x-dx[i],y-dy[i])+1);
				m[y][x]=1;
				break;
			}
		}
	}
	return res;
}
int main()
{
	int i,j,t,x,y;
	while(scanf("%d%d",&w,&h),w)
	{
		memset(m,0,sizeof(m));
		for(i=1;i<=h;++i)for(j=1;j<=w;++j)
		{
			scanf("%d",&t);
			if(t-2)m[i][j]=t;
			else x=j,y=i;
		}
		t=F(0,x,y);
		if(t>10)t=-1;
		printf("%d\n",t);
	}
	return 0;
}