#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<ll> vll;
typedef pair<int,int> pint;

#define DE 1
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define ALL(s) (s).begin(),(s).end()
#define REP(i,n) for (int i = 0; i < (int)(n); ++i)
#define EACH(i,s) for (typeof((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl

template<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<", "<<P.second<<'>';}
template<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<"{ ";for(int i=0;i<P.size();++i){if(i>0)s<<", ";s<<P[i];}return s<<" }"<<endl;}
template<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<"{ ";for(typeof(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<", ";s<<'<'<<it->first<<"->"<<it->second<<'>';}return s<<" }"<<endl;}
template<class T> pair<T,T> operator+(pair<T,T> P, pair<T,T> Q) {return make_pair(P.first+Q.first,P.second+Q.second);}
template<class T> pair<T,T> operator-(pair<T,T> P, pair<T,T> Q) {return make_pair(P.first-Q.first,P.second-Q.second);}
template<class T1, class T2> pair<T1,T1> operator*(pair<T1,T1> P, T2 a) {return make_pair(P.first*a,P.second*a);}



int W, H;
pint s,t;

pint dpos[4] = {MP(1,0), MP(0,1), MP(-1,0), MP(0,-1)};
int field[30][30];

int res = 100;
void rec(pint pos, int num) {
//    cout << endl;
//    COUT(pos);
//    COUT(num);
    
    if (num > 10) return;
    if (num > res) return;
    
    for (int i = 0; i < 4; ++i) {
        pint npos = pos + dpos[i];
        if (npos == t) {
            res = min(res, num + 1); 
            return;
        }
        else if (npos.FI < 0 || npos.FI >= H || npos.SE < 0 || npos.SE >= W) continue;
        else if (field[npos.FI][npos.SE] == 1) continue;
        
        while (true) {
            npos = npos + dpos[i];
            if (npos == t) {
                res = min(res, num + 1);
                return;
            }
            else if (npos.FI < 0 || npos.FI >= H || npos.SE < 0 || npos.SE >= W) {
                break;
            }
            else if (field[npos.FI][npos.SE] == 1) {
                field[npos.FI][npos.SE] = 0;
                
                rec(npos - dpos[i], num+1);
                
                field[npos.FI][npos.SE] = 1;
                break;
            }
        }
    }
}

int main() {
    //freopen( "/Users/macuser/Documents/Programming/Contest/input.in", "r", stdin );
    
    while (cin >> W >> H) {
        if (W == 0 && H == 0) break;
        
        memset(field, 0, sizeof(field));
        int in;
        for (int i = 0; i < H; ++i) {
            for (int j = 0; j < W; ++j) {
                cin >> in;
                if (in == 1) field[i][j] = in;
                if (in == 2) s = MP(i, j);
                if (in == 3) t = MP(i, j);
            }
        }
        
        res = 100;
        rec(s, 0);
        
        if (res <= 10) cout << res << endl;
        else cout << -1 << endl;
    }
    
    return 0;
}


Measured memory_bytes: 1164