#include<iostream>
#include<queue>
#include<vector>
#include<tuple>
#include<algorithm>
using namespace std;

#define MAX_N 25
#define INF 1000000000

int x[MAX_N][MAX_N];
int H, W, c, sy, sx, gy, gx;
int power[MAX_N];
int R[MAX_N][MAX_N];
queue<tuple<int, int, int, vector<int>>>Q;//y,x,ti,map;

int main() {
	while (true) {
		for (int i = 0; i < MAX_N; i++) {
			for (int j = 0; j < MAX_N; j++) {
				x[i][j] = 0;
				R[i][j] = INF;
			}
		}
		sy = 0; sx = 0; gy = 0; gx = 0;
		cin >> W >> H;
		if (H == 0 && W == 0) {
			break;
		}
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> c;
				if (c == 0) {
					x[i][j] = 0;
				}
				if (c == 1) {
					x[i][j] = 1;
				}
				if (c == 2) {
					sy = i; sx = j;
					x[i][j] = 0;
				}
				if (c == 3) {
					gy = i; gx = j;
					x[i][j] = 0;
				}
			}
		}
		power[0] = 1;
		for (int i = 1; i < MAX_N; i++) {
			power[i] = power[i - 1] * 2;
		}
		vector<int>vec;
		for (int i = 0; i < H; i++) {
			int cnt = 0;
			for (int j = 0; j < W; j++) {
				cnt += power[j] * x[i][j];
			}
			vec.push_back(cnt);
		}

		Q.push(make_tuple(sy, sx, 0, vec));
		int a1, a2, a3, cy, cx, pw, y[MAX_N][MAX_N];
		vector<int>a4;
		tuple<int, int, int, vector<int>>tup;

		int res = -1;
		while (!Q.empty()) {
			tup = Q.front();
			Q.pop();
			int a1 = get<0>(tup);
			int a2 = get<1>(tup);
			int a3 = get<2>(tup);
			if (a3 >= 10) { res = -1; goto E; }
			a4 = get<3>(tup);
			for (int i = 0; i < H; i++) {
				for (int j = 0; j < W; j++) {
					y[i][j] = (a4[i] / power[j]) % 2;
				}
			}

			//up.
			cy = a1; cx = a2;
			if (cy == 0) {
				goto T1;
			}
			if (y[cy - 1][cx] == 1) {
				goto T1;
			}
			while (cy >= 0) {
				if (cy == gy && cx == gx) {
					res = a3 + 1;
					goto E;
				}
				else if (cy == 0) {
					goto G;
				}
				else if (y[cy - 1][cx] == 0) {
					cy--;
				}
				else {
					pw = power[cx];
					a4[cy - 1] -= pw;
					R[cy][cx] = min(R[cy][cx], a3 + 1);
					Q.push(make_tuple(cy, cx, a3 + 1, a4));
					a4[cy - 1] += pw;
					break;
				}
				goto G2;
			G:;
				cy--;
			G2:;
			}
		T1:;
			cy = a1; cx = a2;

			//down.
			if (cy == H - 1) {
				goto T2;
			}
			if (y[cy + 1][cx] == 1) {
				goto T2;
			}
			while (cy < H) {
				if (cy == gy && cx == gx) {
					res = a3 + 1;
					goto E;
				}
				else if (cy == H - 1) {
					goto I;
				}
				else if (y[cy + 1][cx] == 0) {
					cy++;
				}
				else {
					pw = power[cx];
					a4[cy + 1] -= pw;
					R[cy][cx] = min(R[cy][cx], a3 + 1);
					Q.push(make_tuple(cy, cx, a3 + 1, a4));
					a4[cy + 1] += pw;
					break;
				}
				goto I2;
			I:;
				cy++;
			I2:;
			}
		T2:;

			//left.
			cy = a1; cx = a2;
			if (cx == 0) {
				goto T3;
			}
			if (y[cy][cx - 1] == 1) {
				goto T3;
			}
			while (cx >= 0) {
				if (cy == gy && cx == gx) {
					res = a3 + 1;
					goto E;
				}
				else if (cx == 0) {
					goto J;
				}
				else if (y[cy][cx - 1] == 0) {
					cx--;
				}
				else {
					pw = power[cx - 1];
					a4[cy] -= pw;
					R[cy][cx] = min(R[cy][cx], a3 + 1);
					Q.push(make_tuple(cy, cx, a3 + 1, a4));
					a4[cy] += pw;
					break;
				}
				goto J2;
			J:;
				cx--;
			J2:;
			}
		T3:;
			cy = a1; cx = a2;

			//right.
			if (cx == W - 1) {
				goto T4;
			}
			if (y[cy][cx + 1] == 1) {
				goto T4;
			}
			while (cx < W) {
				if (cy == gy && cx == gx) {
					res = a3 + 1;
					goto E;
				}
				else if (cx == W - 1) {
					goto K;
				}
				else if (y[cy][cx + 1] == 0) {
					cx++;
				}
				else {
					pw = power[cx + 1];
					a4[cy] -= pw;
					R[cy][cx] = min(R[cy][cx], a3 + 1);
					Q.push(make_tuple(cy, cx, a3 + 1, a4));
					a4[cy] += pw;
					break;
				}
				goto K2;
			K:;
				cx++;
			K2:;
			}
		T4:;
		}
	E:;
		while (!Q.empty()) {
			Q.pop();
		}
		cout << res << endl;
	}
	return 0;
}

Measured memory_bytes: 12908