#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int, vector<vector<int>>> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

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

int main(){
    while(1){
        int w, h; cin>>w>>h;
        if(w == 0) return 0;
        vector<vector<int>> s(h, vector<int>(w));
        int sy, sx, gy, gx;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin>>s[i][j];
                if(s[i][j] == 2){
                    s[i][j] = 0;
                    sy = i;
                    sx = j;
                }
                if(s[i][j] == 3){
                    s[i][j] = 0;
                    gy = i;
                    gx = j;
                }
            }
        }

        queue<T> que;
        que.push(T(sy, sx, 0, s));
        while(que.size()){
            int cy, cx, cnt;
            vector<vector<int>> cs;
            tie(cy, cx, cnt, cs) = que.front();
            que.pop();

            for(int j=cx+1; j<w; j++){
                if(cy == gy && j == gx){
                    cout << cnt + 1 << endl;
                    goto END;
                }

                if(cs[cy][j] == 1){
                    if(cx + 1 < w && cs[cy][cx+1] == 0 && 2 <= j - cx){
                        cs[cy][j] = 0;
                        if(cnt <= 8) que.push(T(cy, j-1, cnt+1, cs));
                        cs[cy][j] = 1;
                    }
                    break;
                }
            }

            for(int j=cy+1; j<h; j++){
                if(j == gy && cx == gx){
                    cout << cnt + 1 << endl;
                    goto END;
                }

                if(cs[j][cx] == 1){
                    if(cy + 1 < h && cs[cy+1][cx] == 0 && 2 <= j - cy){
                        cs[j][cx] = 0;
                        if(cnt <= 8) que.push(T(j-1, cx, cnt+1, cs));
                        cs[j][cx] = 1;
                    }
                    break;
                }
            }

            for(int j=cx-1; j>=0; j--){
                if(cy == gy && j == gx){
                    cout << cnt + 1 << endl;
                    goto END;
                }

                if(cs[cy][j] == 1){
                    if(0 <= cx - 1 && cs[cy][cx-1] == 0 && 2 <= cx - j){
                        cs[cy][j] = 0;
                        if(cnt <= 8) que.push(T(cy, j+1, cnt+1, cs));
                        cs[cy][j] = 1;
                    }
                    break;
                }
            }
                        
            for(int j=cy-1; j>=0; j--){
                if(j == gy && cx == gx){
                    cout << cnt + 1 << endl;
                    goto END;
                }

                if(cs[j][cx] == 1){
                    if(0 <= cy - 1 && cs[cy-1][cx] == 0 && 2 <= cy - j){
                        cs[j][cx] = 0;
                        if(cnt <= 8) que.push(T(j+1, cx, cnt+1, cs));
                        cs[j][cx] = 1;
                    }
                    break;
                }
            }
        }

        cout << -1 << endl;
        END:;
    }
}


Measured memory_bytes: 65432