//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int tab[30][30];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,-1,1};
int W, H;
int Gx, Gy;

bool out(int x, int y){
  return !(0 <= x && x < W && 0 <= y && y < H);
}

int dfs(int x, int y, int d){
  if(d > 10) return d;

  int res = 11;
  REP(i,4){
	int tx = x+dx[i], ty = y+dy[i];
	if(out(tx,ty) || tab[ty][tx] != 0)
	  continue;
	
	while(!out(tx,ty) && tab[ty][tx] == 0){
	  if(tx == Gx && ty == Gy)
		return d+1;
	  tx += dx[i], ty += dy[i];
	}
	if(out(tx,ty)) continue;

	tab[ty][tx] = 0;
	res = min(res, dfs(tx-dx[i], ty-dy[i], d+1));
	tab[ty][tx] = 1;
  }
  return res;
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while(cin>>W>>H,W){
	fill((int*)tab,(int*)tab+30*30,0);
	int sx, sy;
	REP(y,H) REP(x,W){
	  cin >> tab[y][x];
	  if(tab[y][x] == 2)
		sx = x, sy = y, tab[y][x] = 0;
	  if(tab[y][x] == 3)
		Gx = x, Gy = y, tab[y][x] = 0;
	}
	int ans = dfs(sx, sy, 0);
	if(ans > 10) cout << -1 << endl;
	else cout << ans << endl;
  }
  
  return 0;
}
#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;
}