#include <bits/stdc++.h>
using namespace std;

// type {{{
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
// }}}


// macro {{{
#define REP(i,n) for (int i=0; i<(n); ++i)
#define RREP(i,n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i,a,n) for (int i=(a); i<(n); ++i)
#define RFOR(i,a,n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define all(x) begin(x),end(x)
// }}}


// debug {{{
#define dump(x) cerr<<#x<<" = "<<(x)<<endl
#define debug(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "[";
    REP (i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "(" << p.first << " " << p.second << ")";
}
// }}}


// chmax, chmin {{{
template<class T>
bool chmax(T& a, const T& b) {
    if (a < b) { a = b; return true; }
    return false;
}
template<class T>
bool chmin(T& a, const T& b) {
    if (b < a) { a = b; return true; }
    return false;
}
// }}}


// constants {{{
#define inf(T) (numeric_limits<T>::max() / 2)
const ll MOD = 1e9+7;
const ld EPS = 1e-9;
// }}}



int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    for (;;) {
        int W, H; cin >> W >> H;
        if (W == 0) break;
        vvi board(H, vi(W));
        int si, sj, gi, gj;
        REP (i, H) {
            REP (j, W) {
                cin >> board[i][j];
                if (board[i][j] == 2) {
                    si = i, sj = j;
                    board[i][j] = 0;
                }
                if (board[i][j] == 3) {
                    gi = i, gj = j;
                    board[i][j] = 0;
                }
            }
        }

        int ans = inf(int);

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

        auto dfs = [&](auto dfs, vvi& board, int i, int j, int d) {
            if (i == gi and j == gj) {
                chmin(ans, d);
                return;
            }
            if (d >= 10) return;
            REP (k, 4) {
                int y = i, x = j;
                if (y + dy[k] < 0 or H <= y + dy[k] or
                        x + dx[k] < 0 or W <= x + dx[k] or
                        board[y + dy[k]][x + dx[k]] == 1) {
                    continue;
                }
                for (;;) {
                    int ny = y + dy[k], nx = x + dx[k];
                    if (ny < 0 or H <= ny or nx < 0 or W <= nx) {
                        break;
                    }
                    if (board[ny][nx] == 1) {
                        board[ny][nx] = 0;
                        dfs(dfs, board, y, x, d + 1);
                        board[ny][nx] = 1;
                        break;
                    }
                    if (ny == gi and nx == gj) {
                        dfs(dfs, board, ny, nx, d + 1);
                        break;
                    }
                    swap(y, ny);
                    swap(x, nx);
                }
            }
        };

        dfs(dfs, board, si, sj, 0);

        if (ans == inf(int)) {
            ans = -1;
        }
        cout << ans << endl;
    }

    return 0;
}



Measured memory_bytes: 3100