#include <bits/stdc++.h>
                                        #include<iostream>
                                        #include<cstdio>
                                        #include<vector>
                                        #include<queue>
                                        #include<map>
                                        #include<cstring>
                                        #include<string>
                                        #include <math.h>
                                        #include<algorithm>
                                    //    #include <boost/multiprecision/cpp_int.hpp>
                                        #include<functional>
                                #define int long long
                                        #define inf  1000000007
                                        #define pa pair<int,int>
                                        #define ll long long
                                        #define pal pair<double,pa>
                                        #define ppa pair<int,int>
                                        #define ppap pair<int,pa>
                                        #define ssa pair<string,int>
                                        #define  mp make_pair
                                        #define  pb push_back
                                        #define EPS (1e-12)
                                        #define equals(a,b) (fabs((a)-(b))<EPS)
                                  #define VI vector<int>
                                        using namespace std;
                                        
                                  class pas{
                                    	public:
                                    	int x,y;
                                    	pas(int x=0,int y=0):x(x),y(y) {}
                                    	pas operator + (pas p) {return pas(x+p.x,y+p.y);}
                                    	pas operator - (pas p) {return pas(x-p.x,y-p.y);}
                                    	pas operator * (int a) {return pas(x*a,y*a);}
                                    	pas operator / (int a) {return pas(x/a,y/a);}
                                    //	double absv() {return sqrt(norm());}
                                    	int norm() {return x*x+y*y;}
                                    	bool operator < (const pas &p) const{
                                   		return x != p.x ? x<p.x: y<p.y;
                                   	}
                                  //		bool operator < (const pas &p) const{
                                    //		return y != p.y ? y<p.y: x<p.x;
                                    //	}
                                    	bool operator == (const pas &p) const{
                                    		return x==p.x && y==p.y;
                                    	}
                                    };

                                        class Point{
                                        	public:
                                        	double x,y;
                                        	Point(double x=0,double y=0):x(x),y(y) {}
                                        	Point operator + (Point p) {return Point(x+p.x,y+p.y);}
                                        	Point operator - (Point p) {return Point(x-p.x,y-p.y);}
                                        	Point operator * (double a) {return Point(x*a,y*a);}
                                        	Point operator / (double a) {return Point(x/a,y/a);}
                                        	double absv() {return sqrt(norm());}
                                        	double norm() {return x*x+y*y;}
                                        	bool operator < (const Point &p) const{
                                        		return x != p.x ? x<p.x: y<p.y;
                                        	}
                                        	bool operator == (const Point &p) const{
                                        		return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
                                        	}
                                        };
                                        typedef Point Vector;
                                 #define pl pair<int,pas>
                                        struct Segment{
                                        Point p1,p2;
                                        };
                                         double dot(Vector a,Vector b){
                                        	return a.x*b.x+a.y*b.y;
                                        }
                                        double cross(Vector a,Vector b){
                                        	return a.x*b.y-a.y*b.x;
                                        }
                                    
            bool parareru(Point a,Point b,Point c,Point d){
            //	if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl;
            	return abs(cross(a-b,d-c))<EPS;
            }
            double distance_ls_p(Point a, Point b, Point c) {
              if ( dot(b-a, c-a) < EPS ) return (c-a).absv();
              if ( dot(a-b, c-b) < EPS ) return (c-b).absv();
              return abs(cross(b-a, c-a)) / (b-a).absv();
            }
            bool is_intersected_ls(Segment a,Segment b) {
            	if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false;
            	if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){
            //		cout<<"sss"<<endl;
            		if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true;
            		if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true;
            		if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true;
            		if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true;
            		return false;
            	}
              else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS );
            }
            double segment_len(Segment a){
            	
            	return (a.p1-a.p2).absv();
            }
            double segment_dis(Segment a,Segment b){
            	if(is_intersected_ls(a,b))return 0;
            	double r=distance_ls_p(a.p1, a.p2, b.p1);
            	r=min(r,distance_ls_p(a.p1, a.p2, b.p2));
            	r=min(r,distance_ls_p(b.p1, b.p2, a.p2));
            	r=min(r,distance_ls_p(b.p1, b.p2, a.p1));
            	return r;
            }
            Point intersection_ls(Segment a, Segment b) {
              Point ba = b.p2-b.p1;
              double d1 = abs(cross(ba, a.p1-b.p1));
              double d2 = abs(cross(ba, a.p2-b.p1));
              double t = d1 / (d1 + d2);
             
              return a.p1 + (a.p2-a.p1) * t;
            }
			pair<Point,Point> circle_intersection(Point c1,double r1,Point c2,double r2){
				double d=(c1-c2).absv();
				double h=(r1*r1-r2*r2+d*d)/2.0/d;
				double l=sqrt(r1*r1-h*h);
			//	cout<<d<<" "<<h<<" "<<l<<endl;
				Point asi=c1+(c2-c1)*h/((c2-c1).absv());
				Vector r1r2=(c2-c1)/((c2-c1).absv());
				Vector sui={r1r2.y,-r1r2.x};
			//	cout<<sui.x<<" "<<sui.y<<endl;
				pair<Point,Point> z=mp(asi+sui*l,asi-sui*l);
				if(z.first.x>z.second.x) swap(z.first,z.second);
				return z;
			}

int par[500100],ranks[500100],kosuu[500100];     

void shoki(int n){
	for(int i=0;i<n;i++){
		par[i]=i;
		ranks[i]=0;
		kosuu[i]=1;
	}
}

int root(int x){
	return par[x]==x ? x : par[x]=root(par[x]);
}

bool same(int x,int y){
return root(x)==root(y);
}

void unite(int x,int y){
 x=root(x);
 y=root(y);
	int xx=kosuu[x],yy=kosuu[y];
 if(x==y) return;
	if(ranks[x]<ranks[y]){
		par[x]=y;
		kosuu[y]=yy+xx;
	}
 else {
	par[y]=x;
	if(ranks[x]==ranks[y]) ranks[x]=ranks[x]+1;
 	kosuu[x]=yy+xx;
 }
	return;
}


/* ---memo---
shoki(n)でn個の点に対する初期化を必ず。
unite(a,b)でaとbを同じグループに入れる
same(a,b)がtrueなら同グループ,falseなら別。
*/
int a[30][30]={0};

int x,y,gx,gy;

int ans;

void dfs(int r){
	if(r>10) return;
	int hx=x,hy=y;
	int dx=x,dy=y;
	if(a[x+1][y]!=1){
	while(a[dx][y]!=5 && a[dx][y] !=3 &&a[dx][y]!=1){
		dx++;
	}
	if(a[dx][y]==3){
		ans=min(ans,r);
		return;
	}
	if(a[dx][y]==1){
		dx--;
		x=dx;
		a[x+1][y]=0;
		dfs(r+1);
		a[x+1][y]=1;
		x=hx,y=hy;
	}
	}
	x=hx,y=hy;
	
	dx=x,dy=y;
	if(a[x-1][y]!=1){
	while(a[dx][y]!=5 && a[dx][y] !=3 &&a[dx][y]!=1){
		dx--;
	}
	if(a[dx][y]==3){
		ans=min(ans,r);
		return;
	}
	if(a[dx][y]==1){
		dx++;
		x=dx;
		a[x-1][y]=0;
		dfs(r+1);
		a[x-1][y]=1;
		x=hx,y=hy;
	}
	}
	x=hx,y=hy;
	dx=x,dy=y;
	if(a[x][y+1]!=1){
	while(a[x][dy]!=5 && a[x][dy] !=3 &&a[x][dy]!=1){
		dy++;
	}
	if(a[x][dy]==3){
		ans=min(ans,r);
		return;
	}
	if(a[x][dy]==1){
		dy--;
		y=dy;
		a[x][y+1]=0;
		dfs(r+1);
		a[x][y+1]=1;
		x=hx,y=hy;
	}
	
	}
	x=hx,y=hy;
	dx=x,dy=y;
	if(a[x][y-1]!=1){
	while(a[x][dy]!=5 && a[x][dy] !=3 &&a[x][dy]!=1){
		dy--;
	}
	if(a[x][dy]==3){
		ans=min(ans,r);
		return;
	}
	if(a[x][dy]==1){
		dy++;
		y=dy;
		a[x][y-1]=0;
		dfs(r+1);
		a[x][y-1]=1;
		x=hx,y=hy;
	}
	}
}

     signed main(){
int w,h;
     	while(1){
     		cin>>w>>h;
     		if(w==0 && h==0) return 0;
     		
     		for(int i=0;i<22;i++)for(int j=0;j<22;j++)a[i][j]=0;
     		for(int i=0;i<=h+1;i++)a[i][0]=5,a[i][w+1]=5;
     		for(int i=0;i<=w+1;i++)a[0][i]=5,a[h+1][i]=5;
     		for(int i=1;i<=h;i++)for(int j=1;j<=w;j++){
     			cin>>a[i][j];
     			if(a[i][j]==2) x=i,y=j;
     			if(a[i][j]==3) gx=i,gy=j;
     		}
     		ans=11;
     		dfs(1);
     		if(ans<=10)cout<<ans<<endl;
     		else cout<<-1<<endl;
     	}  
           	return 0;
           }


Measured memory_bytes: 2964