#include <iostream>
#include <utility>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <stack>

using namespace std;

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

const int INF = 1 << 28;

int w, h;
int field[32][32];
int sx, sy;
int gx, gy;

int ofs[4][2] = {
  {0,1},
  {1,0},
  {-1,0},
  {0,-1}
};

bool ok(int p, int n)
{
  return 0 <= p && p < n;
}

bool dfs(int x, int y, int d, int n)
{
  if( d >= n ) {
    return false;
  }

  bool res = false;
  rep(i, 4) {
    int dx = ofs[i][0];
    int dy = ofs[i][1];
    int nx = x;
    int ny = y;
    int idx = 0;
    while(ok(nx, w) && ok(ny, h) && !field[ny][nx]) {
      nx += dx;
      ny += dy;
      if(nx == gx && ny == gy) {
        return true;
      }
      ++idx;
    }
    if( idx > 1 && ok(nx, w) && ok(ny, h) ) {
      field[ny][nx] = 0;
      res |= dfs(nx - dx, ny - dy, d + 1, n);
      field[ny][nx] = 1;
    }
  }
  return res;
}

int idfs(int sx, int sy)
{
  int idx = 1;
  while( idx <= 10 && !dfs(sx, sy, 0, idx) ) {
    ++idx;
  }
  if( idx > 10 ) {
    return -1;
  } else {
    return idx;
  }
}

int main()
{
  while( cin >> w >> h, (h|w) ) {
    rep(i, h) rep(j, w) {
      int v;
      cin >> v;
      field[i][j] = v & 1;
      if( v & 2 ) {
        if( v & 1 ) {
          gx = j;
          gy = i;
        } else {
          sx = j;
          sy = i;
        }
      }
    }

    cout << idfs(sx, sy) << endl;
  }
  return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int w, h;
int board[21][21];
int ans = INF;
bool valid(int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; }
void print() {
    rep(y, h) {
        rep(x, w) printf("%d ", board[y][x]);
        printf("\n");
    }
}
void dfs(int y, int x, int cnt) {
    // printf("debug: %d %d %d\n", y, x, cnt);
    // print();
    if (cnt == 10) return;
    rep(k, 4) {
        int ny = y;
        int nx = x;
        if (!valid(ny + dy[k], nx + dx[k])) continue;
        if (board[ny + dy[k]][nx + dx[k]] == 1) continue;
        while (1) {
            if (!valid(ny + dy[k], nx + dx[k])) break;
            ny += dy[k];
            nx += dx[k];
            if (board[ny][nx] == 3) {
                ans = min(cnt + 1, ans);
                return;
            }
            if (board[ny][nx] == 1) {
                board[ny][nx] = 0;
                dfs(ny - dy[k], nx - dx[k], cnt + 1);
                board[ny][nx] = 1;
                break;
            }
        }
    }
}
int main() {
    while (cin >> w >> h, w) {
        ans = INF;
        rep(i, h) rep(j, w) cin >> board[i][j];
        rep(y, h) rep(x, w) if (board[y][x] == 2) dfs(y, x, 0);
        if (ans == INF) ans = -1;
        cout << ans << endl;
    }
    return 0;
}