#include<bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int, int>P;

struct st {
	int f[20][20], cnt, x, y, id;
};
int dx[]{ 1,-1,0,0 }, dy[]{ 0,0,1,-1 };
int main() {
	st in;
	int w, h;
	while (scanf("%d%d", &w, &h), w) {
		rep(i, h)rep(j, w) {
			scanf("%d", &in.f[i][j]);
			if (in.f[i][j] == 2)in.x = i, in.y = j;
		}
		in.cnt = 0;
		in.id = -1;
		queue<st>que;
		que.push(in);
		int Min = INT_MAX;
		while (!que.empty()) {
			st s = que.front(); que.pop();
			if (s.f[s.x][s.y] == 3) {
				Min = min(Min, s.cnt);
				continue;
			}
			if (s.id == -1) {
				rep(i, 4) {
					int nx = s.x + dx[i], ny = s.y + dy[i];
					if (0 <= nx&&nx < h && 0 <= ny&&ny < w&&s.f[nx][ny] != 1) {
						st p = s; p.cnt++; p.x = nx; p.y = ny; p.id = i;
						if (p.cnt <= 10 && p.cnt < Min)que.push(p);
					}
				}
			}
			else {
				int nx = s.x + dx[s.id], ny = s.y + dy[s.id];
				if (0 <= nx&&nx < h && 0 <= ny&&ny < w) {
					if (s.f[nx][ny] == 1) {
						st p = s; p.f[nx][ny] = 0; p.id = -1;
						que.push(p);
					}
					else {
						st p = s; p.x = nx; p.y = ny;
						que.push(p);
					}
				}
			}
		}
		if (Min == INT_MAX)puts("-1");
		else printf("%d\n", Min);
	}
}
#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
#define reep(i,a,b) for(int i=a; i<b; i++)
#define fi first
#define se second

using pii = pair<int,int>;
using vint = vector<char>;
using vvint = vector<vector<char>>;
using bit = bitset<400>;

int w,h;
bool inside(int a, int b){
	return 0<=a&&a<h&&0<=b&&b<w;	
}

int dd[] = {0,1,0,-1,0};


int main(){
	while(cin>>w>>h,w){
		pii start;
		pii goal;
		bit vv(0);		
		rep(i,h) rep(j,w){
			int t;
			cin>>t;
			if(t==1) vv[i*20+j]=1;
			if(t==2){
				start = pii(i,j);
			}
			if(t==3){
				goal = pii(i,j);
			}
		}
		bool ok = false;
		queue<pair<pii,bit>> q;
		q.push(make_pair(start, vv));
		int ans = 20;
		rep(_,11){
			int qs = q.size();
			rep(i,qs){
				pii now = q.front().fi;
				bit vv = q.front().se;
				// rep(ii,h){
				// 	rep(jj,w){
				// 		cout<<vv[ii][jj]<<" ";
				// 	}
				// 	cout<<endl;
				// }
				q.pop();
				rep(j,4){
					pii next = now;
					next.fi += dd[j];
					next.se += dd[j+1];
					// cout<<next.fi<<" "<<next.se<<" "<<inside(next.fi, next.se)<<endl;
					if(inside(next.fi, next.se) && vv[next.fi*20+next.se]==0){
						// cout<<"hoge"<<endl;
						while(inside(next.fi, next.se)&&vv[next.fi*20+next.se]==0){
							// cout<<"vv "<<vv[next.fi*20+next.se]<<endl;
							if(next == goal) ans = min(ans, _+1);
							next.fi += dd[j];
							next.se += dd[j+1];
						}
						if(inside(next.fi, next.se)){
							vv[next.fi*20+next.se] = 0;
							next.fi -= dd[j];
							next.se -= dd[j+1];
							q.push(make_pair(next, vv));
							next.fi += dd[j];
							next.se += dd[j+1];
							vv[next.fi*20+next.se] = 1;
						}
					}
				}
			}
		}
		cout<<(ans<=10?ans:-1)<<endl;
	}
}