#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

ll w,h;
ll sx,sy;
ll gx,gy;
ll ans[21][21];
ll dx[] = {0,1,0,-1};
ll dy[] = {1,0,-1,0};
ll field[21][21];

bool is_in(int y,int x){
	return y >= 0 && y < h && x >= 0 && x < w;
}

// queue<ST> que;

// void solve(ST ss){
// 	queue<ST>().swap(que);
// 	que.push(ss);
// 	while(!que.empty()){
// 		ST s = que.front();
// 		que.pop();
// 		ll cx = s.nx;
// 		ll cy = s.ny;
// 		ll cnt = s.cnt;
// 		// cout << cnt << " " << cy << " "  << cx << endl;
// 		ans[cy][cx] = cnt;
// 		if(cx == gx && cy == gy)break;
// 		if(cnt >= 10)continue;
// 		rep(i,4){
// 			ST nxt;
// 			ll nx = cx + dx[i];
// 			ll ny = cy + dy[i];
// 			// cout << "1!" << ny << " " << nx << endl;
// 			if(!is_in(ny,nx))continue;
// 			if(s.field[ny][nx] == 1)continue;
// 			while(is_in(ny,nx) && s.field[ny][nx] == 0){
// 				ny += dy[i];
// 				nx += dx[i];
// 			}
// 			// cout << "2!" << ny << " " << nx << endl;
// 			if(!is_in(ny,nx))continue;

// 			if(s.field[ny][nx] == 3){
// 				// cout << "!!!" << cnt << " " << ny << " " << nx << endl;
// 				nxt.ny = ny;
// 				nxt.nx = nx;
// 				rep(j,h)rep(k,w)nxt.field[j][k] = s.field[j][k];
// 				nxt.cnt = cnt+1;
// 				que.push(nxt);
// 				continue;
// 			}

// 			rep(j,h)rep(k,w){
// 				if(j != ny || k != nx)nxt.field[j][k] = s.field[j][k];
// 				else nxt.field[j][k] = 0;
// 			}
// 			nxt.ny = ny - dy[i];
// 			nxt.nx = nx - dx[i];
// 			nxt.cnt = cnt+1;
// 			que.push(nxt);
// 		}
// 	}
// }

ll solve(ll cy,ll cx,ll cnt){
	// if(cx == gx && cy == gy)return 0;
	if(cnt == 0)return INF;
	ll res = INF;
	rep(i,4){
		ll nx = cx + dx[i];
		ll ny = cy + dy[i];
		if(!is_in(ny,nx))continue;
		if(field[ny][nx] == 1)continue;
		while(is_in(ny,nx) && field[ny][nx] == 0){
			ny += dy[i];
			nx += dx[i];
		}
		// cout << "2!" << ny << " " << nx << endl;
		if(!is_in(ny,nx))continue;
		if(field[ny][nx] == 3)return 1;

		field[ny][nx] = 0;
		ny -= dy[i];
		nx -= dx[i];
		res = min(solve(ny,nx,cnt - 1)+1,res);
		ny += dy[i];
		nx += dx[i];
		field[ny][nx] = 1;
	}
	return res;
}

int main(){
	while(cin >> w >> h && w+h){
		rep(i,h){
			ll num;
			rep(j,w){
				cin >> num;
				if(num == 1)field[i][j] = 1;
				else {
					field[i][j] = 0;
					if(num == 2)sx = j,sy = i;
					if(num == 3)field[i][j] = 3;
				}
			}
		}
		// cout << "!" << sx << " " << sy << endl;
		ll num = solve(sy,sx,10);
		if(num == INF)cout << -1 << endl;
		else cout << num << endl;
	}
}


Measured memory_bytes: 3052