#include <cstdio>
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <functional>
#include <numeric>
#include <chrono>
#include <cstdlib>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
const double pi = acos(-1);
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define EPS 1e-4
// bool operator<(const pair<int, int> &a, const pair<int, int> &b)
// {
//     if (a.first == b.first)
//         return a.second < b.second;
//     return a.first < b.first;
// };

/*unsigned int xor128(void)
{
    static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned int t = (x ^ (x << 11));
    x = y;
    y = z;
    z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
unsigned int xor128rnd(unsigned int m)
{
    return xor128() % m;
}*/

bool operator<(const pair<int, int> &a, const pair<int, int> &b)
{
    if (a.first == b.first)
    {
        return a.second < b.second;
    }
    return a.first < b.first;
}
int dxy[5] = {-1, 0, 1, 0, -1};
int sx, sy, gx, gy;
int ans = 10000;
int w, h;
void dfs(int y, int x, int cnt, vector<vector<int>> &v)
{
    if (cnt > 10)
        return;

    if (y - 1 >= 0 && v[y - 1][x] != 1)
    {
        int ty = y, tx = x;

        while (ty - 1 >= 0)
        {
            if (ty - 1 == gy && tx == gx)
            {
                ans = min(ans, cnt);
                return;
            }
            if (v[ty - 1][x] == 1)
            {
                v[ty - 1][x] = 0;
                dfs(ty, x, cnt + 1, v);
                v[ty - 1][x] = 1;
                break;
            }
            ty--;
        }
    }
    if (y + 1 < h && v[y + 1][x] != 1)
    {
        int ty = y, tx = x;
        while (ty + 1 < h)
        {
            if (ty + 1 == gy && tx == gx)
            {
                ans = min(ans, cnt);
                return;
            }
            if (v[ty + 1][x] == 1)
            {
                v[ty + 1][x] = 0;
                dfs(ty, x, cnt + 1, v);
                v[ty + 1][x] = 1;
                break;
            }
            ty++;
        }
    }

    if (x - 1 >= 0 && v[y][x - 1] != 1)
    {
        int ty = y, tx = x;
        while (tx - 1 >= 0)
        {
            if (ty == gy && tx - 1 == gx)
            {
                ans = min(ans, cnt);
                return;
            }
            if (v[y][tx - 1] == 1)
            {
                v[y][tx - 1] = 0;
                dfs(y, tx, cnt + 1, v);
                v[y][tx - 1] = 1;
                break;
            }
            tx--;
        }
    }
    if (x + 1 < w && v[y][x + 1] != 1)
    {
        int ty = y, tx = x;
        while (tx + 1 < w)
        {
            if (ty == gy && tx + 1 == gx)
            {
                ans = min(ans, cnt);
                return;
            }
            if (v[y][tx + 1] == 1)
            {
                v[y][tx + 1] = 0;
                dfs(y, tx, cnt + 1, v);
                v[y][tx + 1] = 1;
                break;
            }
            tx++;
        }
    }
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(8);

    while (cin >> w >> h, w || h)
    {
        ans = 1000000;
        vector<vector<int>> v(h, vector<int>(w, 0));
        for (int i = 0; i < h; ++i)
        {
            for (int j = 0; j < w; ++j)
            {
                cin >> v[i][j];
            }
        }
        for (int i = 0; i < h; ++i)
        {
            for (int j = 0; j < w; ++j)
            {
                if (v[i][j] == 2)
                {
                    sy = i;
                    sx = j;
                }
                else if (v[i][j] == 3)
                {
                    gy = i;
                    gx = j;
                }
            }
        }

        dfs(sy, sx, 1, v);
        if (ans == 1000000)
            cout << -1 << endl;
        else
            cout << ans << endl;
    }

    return 0;
}



Measured memory_bytes: 3088