#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 20 + 8;

int n,m,sx,sy,x,y;
int g[maxn][maxn];
int ans;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};

bool is_in(int x,int y)
{
    return x >= 0 && x < n && y >= 0 && y < m;
}
void dfs(int r,int c,int num)
{
    if(num == 10 || num >= ans) return;
    for(int i = 0; i < 4; ++i){
        int x = r + dx[i];
        int y = c + dy[i];
        if(!is_in(x,y) || g[x][y] == 1) continue;
        while(is_in(x,y) && g[x][y] == 0)
        {
            x += dx[i];
            y += dy[i];
        }
        if(!is_in(x,y)) continue;
       // cout << " a2: " << x-dx[i] <<" "<< y-dy[i] <<"\n";
        if(g[x][y] == 3)
        {
            ans = min(ans, num);
            return;
        }
        g[x][y] = 0;
         dfs(x - dx[i], y - dy[i],num + 1);
         g[x][y] = 1;

    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d", &m, &n) && n + m )
    {
        sx = 0,sy = 0;
        memset(g,0,sizeof g);
        for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < m; ++j)
            {
                scanf("%d",&g[i][j]);
                if(g[i][j] == 2)
                {
                    sx = i; sy = j;
                }
            }
        }
        g[sx][sy] = 0;
        ans = inf;
        dfs(sx,sy,0);
        printf("%d\n", ans == inf ? -1 : ans + 1);
    }
    return 0;
}
/*

6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
0 0

*/
/*
  0 1 2 3 4 5
0 1 0 0 2 1 0
1 1 1 0 0 0 0
2 0 0 0 0 0 3
3 0 0 0 0 0 0
4 1 0 0 0 0 1
5 0 1 1 1 1 1

*/