//#include <bits/stdc++.h>
//#include <stdio.h>
#include<iostream>
#include<cstdio>
#include<bitset>
#include<algorithm>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
#include<cmath>
#include<utility>
#include<functional>
#include<map>
#include<set>
#include<cctype>
#include<fstream>
 
 
#define FOR(i, a, b) for( int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for( int i=(a);i>=(b);i--)
#define LFOR(i, a, b) for( long long int i=(a);i<=(b);i++)
#define LRFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000
 
using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;
 
int dy[5] = { 0,0,1,-1,0 };
int dx[5] = { 1,-1,0,0,0 };

 
int main(void){

	while(1){
		int w,h;
		int sx,sy;
		int gx,gy;
		long long int p;
		int mini=INF;
		string root;

		cin>>w>>h;
		if(w==0&&h==0){
			break;
		}
		int d[20][20];
		int keep[20][20];

		FOR(i,0,h-1){
			FOR(j,0,w-1){
				cin>>d[i][j];
				keep[i][j]=d[i][j];
				if(d[i][j]==2){
					sx=j;
					sy=i;
				}
				if(d[i][j]==3){
					gx=j;
					gy=i;
				}
			}
		}

		int r[10];
		int x,y;
		int now;
		bool flag=false;
		x=sx;
		y=sy;

		for(r[0]=0;r[0]<=3;r[0]++){
			for(r[1]=0;r[1]<=3;r[1]++){
				for(r[2]=0;r[2]<=3;r[2]++){
					for(r[3]=0;r[3]<=3;r[3]++){
						for(r[4]=0;r[4]<=3;r[4]++){
							for(r[5]=0;r[5]<=3;r[5]++){
								for(r[6]=0;r[6]<=3;r[6]++){
									for(r[7]=0;r[7]<=3;r[7]++){
										for(r[8]=0;r[8]<=3;r[8]++){
											for(r[9]=0;r[9]<=3;r[9]++){
												root="";
												FOR(i,0,9){
													root+=to_string(r[i]);
												}
												FOR(j,0,19){
													FOR(k,0,19){
														d[j][k]=keep[j][k];
													}
												}

												flag=false;
												x=sx;
												y=sy;
												//cout<<root<<endl;
												FOR(j,0,9){
													now=j;
													if(x+dx[root[j]-'0']>=0&&x+dx[root[j]-'0']<=w-1&&y+dy[root[j]-'0']>=0&&y+dy[root[j]-'0']<=h-1){
														if(d[y+dy[root[j]-'0']][x+dx[root[j]-'0']]==1){
															flag=true;
															break;
														}
														while(flag==false){
															if(x+dx[root[j]-'0']>=0&&x+dx[root[j]-'0']<=w-1&&y+dy[root[j]-'0']>=0&&y+dy[root[j]-'0']<=h-1){
																if(d[y+dy[root[j]-'0']][x+dx[root[j]-'0']]==1){
																	d[y+dy[root[j]-'0']][x+dx[root[j]-'0']]=0;
																	break;
																}else{
																	x+=dx[root[j]-'0'];
																	y+=dy[root[j]-'0'];
																	if(x==gx&&y==gy){
																		mini=min(mini,j+1);
																		switch(j){
																			case 0:
																				goto end;
																			break;
																			case 1:
																				goto jump0;
																			break;
																			case 2:
																				goto jump1;
																			break;
																			case 3:
																				goto jump2;
																			break;
																			case 4:
																				goto jump3;
																			break;
																			case 5:
																				goto jump4;
																			break;
																			case 6:
																				goto jump5;
																			break;
																			case 7:
																				goto jump6;
																			break;
																			case 8:
																				goto jump7;
																			break;
																			case 9:
																				goto jump8;
																			break;
																		}
																		break;
																	}
																}
															}else{
																flag=true;
																break;
															}
														}
													}else{
														flag=true;
													}
				
													if(flag){
														break;
													}
												}

												if(flag==true){
													if(mini!=INF){
													switch(min(now,mini)){
														case 0:
															goto jump0;
														break;
														case 1:
															goto jump1;
														break;
														case 2:
															goto jump2;
														break;
														case 3:
															goto jump3;
														break;
														case 4:
															goto jump4;
														break;
														case 5:
															goto jump5;
														break;
														case 6:
															goto jump6;
														break;
														case 7:
															goto jump7;
														break;
														case 8:
															goto jump8;
														break;
														case 9:
															goto jump9;
														break;
													}
													}else{
														switch(now){
														case 0:
															goto jump0;
														break;
														case 1:
															goto jump1;
														break;
														case 2:
															goto jump2;
														break;
														case 3:
															goto jump3;
														break;
														case 4:
															goto jump4;
														break;
														case 5:
															goto jump5;
														break;
														case 6:
															goto jump6;
														break;
														case 7:
															goto jump7;
														break;
														case 8:
															goto jump8;
														break;
														case 9:
															goto jump9;
														break;
													}
													}
													
												}

												jump9:;
											}
											jump8:;
										}
										jump7:;
									}
									jump6:;
								}
								jump5:;
							}
							jump4:;
						}
						jump3:;
					}
					jump2:;
				}
				jump1:;
			}
			jump0:;
		}
		end:;


		if(mini==INF){
			cout<<-1<<endl;
		}else{
			cout<<mini<<endl;
		}

		
	}

}



Measured memory_bytes: 3196