#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<(n);i++)
#define loop(i,a,n) for(i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int v[20][20];
int ans;
int h, w;
int gx,gy;

void dfs(int x, int y, int t){
    //cout << "! " << x << " " << y << " " << t << endl;
    if(t > 10)return;
    if(x == gx && y == gy){
        ans = min(ans, t);
        return;
    }
    if(x == gx){
        int i = y;
        int tmp = 1;
        if(y < gy && v[x][y+1] != 1){
            for(i = y; i < gy; i++){
                if(v[x][i] == 1)tmp++;
            }
        }else if(y > gy && v[x][y-1] != 1){
            for(i = y; i > gy; i--){
                if(v[x][i] == 1)tmp++;
            }
        }
        if(i == gy){
            ans = min(ans, t + tmp);
            return;
        }
    }
    if(y == gy){
        int i = x;
        int tmp = 1;
        if(x < gx && v[x+1][y] != 1){
            for(i = x; i < gx; i++){
                if(v[i][y] == 1)tmp++;
            }
        }else if(x > gx && v[x-1][y] != 1){
            for(i = x; i > gx; i--){
                if(v[i][y] == 1)tmp++;
            }
        }
        if(i == gx){
            ans = min(ans, t + tmp);
            return;
        }
    }

    for(int i = x; i < h; i++){
        if(v[i][y] == 1){
            if(x+1 == i)break;
            v[i][y] = 0;
            //cout << i << " " << y << 0 <<" " << endl;
            dfs(i-1, y, t+1);
            //cout << i << " " << y << 1 <<" " << endl;
            v[i][y] = 1;
            break;
        }
    }

    for(int i = x; i >= 0; i--){
        if(v[i][y] == 1){
            if(x-1 == i)break;
            v[i][y] = 0;
            //cout << i << " " << y << 0 <<" " << endl;
            dfs(i+1, y, t+1);
            //cout << i << " " << y << 1 <<" " << endl;
            v[i][y] = 1;
            break;
        }
    }

    for(int i = y; i < w; i++){
        if(v[x][i] == 1){
            if(y+1 == i)break;
            v[x][i] = 0;
            //cout << x << " " << i << 0 <<" " << endl;
            dfs(x, i-1, t+1);
            v[x][i] = 1;
            //cout << x << " " << i << 1 <<" " << endl;
            break;
        }
    }

    for(int i = y; i >= 0; i--){
        if(v[x][i] == 1){
            if(y-1 == i)break;
            v[x][i] = 0;
            //cout << x << " " << i << 0 <<" " << endl;
            dfs(x, i+1, t+1);
            v[x][i] = 1;
            //cout << x << " " << i << 1 <<" " << endl;
            break;
        }
    }
}

int main(void) {
    int i,j;
    while(cin >> w >> h, h){
        int sx, sy;
        rep(i,h)rep(j,w){
            cin >> v[i][j];
            if(v[i][j] == 2){
                sx = i;
                sy = j;
            }
            if(v[i][j] == 3){
                gx = i;
                gy = j;
            }
        }
        ans = INF;
        dfs(sx, sy, 0);
        if(ans <= 10){
            cout << ans << endl;
        }else{
            cout << -1 << endl;
        }
    }
}



Measured memory_bytes: 3076