#include <iostream>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int w, h;
int a[20][20];
int sy, sx, gy, gx;
int ans;

bool outside(int y, int x) {
	if (y < 0 || y >= h || x < 0 || x >= w) return true;
	return false;
}

void dfs(int y, int x, int depth) {
	if (depth >= ans) { return; }
	if (y == gy && x == gx) {
		ans = depth;
		return;
	}
	if (depth >= ans - 1) { return; }
	
	int i;
	rep(i, 4) {
		int py = y + dy[i];
		int px = x + dx[i];
		if (outside(py, px) || a[py][px] == 1) continue;
		while (true) {
			if (outside(py, px)) break;
			if (!outside(py + dy[i], px + dx[i]) && a[py + dy[i]][px + dx[i]] == 1) break;
			if (a[py][px] == 3) break;
			py += dy[i];
			px += dx[i];
		}
		if (outside(py, px)) continue;
		
		if (a[py][px] != 3) { a[py + dy[i]][px + dx[i]] = 0; }
		dfs(py, px, depth + 1);
		if (a[py][px] != 3) { a[py + dy[i]][px + dx[i]] = 1; }
	}
}

int main() {
	while (cin >> w >> h) {
		if (!w) break;
		
		ans = 11;
		
		int i, j;
		rep(i, h) rep(j, w) cin >> a[i][j];
		rep(i, h) {
			rep(j, w) {
				if (a[i][j] == 2) { sy = i; sx = j; }
				if (a[i][j] == 3) { gy = i; gx = j; }
			}
		}
		dfs(sy, sx, 0);
		if (ans == 11) { cout << -1 << endl; }
		else { cout << ans << endl; }
	}
	return 0;
}
