#include <iostream>
#include <vector>

using namespace std;

typedef vector <int> VI;
typedef vector <VI> VVI;

const int dx[5] = { 0, 0, 0, 1, -1 };
const int dy[5] = { 0, 1, -1, 0, 0 };

int w, h;
int sx, sy, gx, gy;
VVI M;
int answer;

//==================================================
// Tõ
//==================================================
void dfs( int x, int y, int a, int d )
{
	if ( d > 10 ) return;

	if ( M[y][x] == 3 )
	{
		if ( answer == -1 )
			answer = d;
		else
			answer = min( answer, d );
	}
	if ( a > 0 )
	{
		int nx = x + dx[a];
		int ny = y + dy[a];
		if ( nx < 0 || nx >= w || ny < 0 || ny >= h )
			return;
		if ( M[ny][nx] == 1 )
		{
			M[ny][nx] = 0;
			dfs( x, y, 0, d );
			M[ny][nx] = 1;
		}
		else
			dfs( nx, ny, a, d );
	}
	else
	{
		for ( int i = 1; i < 5; i++ )
		{
			int nx = x + dx[i];
			int ny = y + dy[i];
			if ( nx < 0 || nx >= w || ny < 0 || ny >= h )
				continue;
			if ( M[ny][nx] == 0 || M[ny][nx] == 3 )
			{
				dfs( nx, ny, i, d+1 );
			}
		}
	}

}

int main( void )
{
	while ( cin >> w >> h && w && h )
	{
		//==================================================
		// Ï
		//==================================================
		M = VVI( h, VI( w, 0 ) );
		answer = -1;

		//==================================================
		// }bvÌüÍ
		//==================================================
		for ( int i = 0; i < h; i++ )
		{
			for ( int j = 0; j < w; j++ )
			{
				int& s = M[i][j];
				cin >> s;

				// X^[gES[
				if ( s == 2 )
				{
					sx = j;
					sy = i;
					s  = 0;
				}
				else if ( s == 3 )
				{
					gx = j;
					gy = i;
				}
			}
		}

		//==================================================
		// 
		//==================================================
		dfs( sx, sy, 0, 0 );

		//==================================================
		// oÍ
		//==================================================
		cout << answer << endl;
	}
	return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;

#define REP(i,n) for(int i = 0; i < (int)(n); ++i)

#define DEBUG(x) cerr << #x << " = " << x << endl
int F[20][20];

const int INF = (int)1e9;
const int dy[4] = { 0, -1, 0, 1 };
const int dx[4] = { 1, 0, -1, 0 };

int W, H;
int ans;

void solve(const int y, const int x, const int depth) {
  if(F[y][x] == 3) {
    ans = min(ans, depth);
    return;
  }
  if(depth >= 10) return;
  for(int k = 0; k < 4; ++k) {
    int ny = y, nx = x;
    if(0 <= ny + dy[k] && ny + dy[k] < H && 0 <= nx + dx[k] && nx + dx[k] < W && F[ny + dy[k]][nx + dx[k]] == 1) continue;
    bool ok = true;
    int sy = -1, sx = -1;
    while(true) {
      if(F[ny][nx] == 3) break;
      ny += dy[k];
      nx += dx[k];
      if(ny < 0 || ny >= H || nx < 0 || nx >= W) {
        ok = false;
        break;
      }
      if(F[ny][nx] == 1) {
        sy = ny;
        sx = nx;
        F[sy][sx] = 0;
        ny -= dy[k];
        nx -= dx[k];
        break;
      }
    }
    if(ok)
      solve(ny,nx,depth+1);
    if(sy != -1 && sx != -1) {
      F[sy][sx] = 1;
    }
  }
}

int main() {
  while(true) {
    cin >> W >> H;
    if(W == 0 && H == 0) break;
    REP(i,H) REP(j,W) cin >> F[i][j];
    ans = INF;
    REP(i,H) REP(j,W) {
      if(F[i][j] == 2) solve(i,j,0);
    }
    if(ans == INF) cout << -1 << endl;
    else cout << ans << endl;
  }
}