#include #include #include using namespace std; int dw_table[] = {1,-1,0,0}; int dh_table[] = {0,0,1,-1}; int ed_h,ed_w; int min_path; void dfs(int h,int w,vector >& board,int depth){ if(depth == 10) return; for(int i=0;i<4;i++){ int nh = h; int nw = w; while(1){ nh += dh_table[i]; nw += dw_table[i]; // cout << nh << "-" << nw << endl; if(nw<0 || nh<0 || nw>=board[0].size() || nh>=board.size()) break; if(board[nh][nw] == 3){ min_path = min(depth+1,min_path); return; } if(board[nh][nw] == 1){ if(nh-dh_table[i] == h && nw-dw_table[i] == w) break; board[nh][nw] = 0; dfs(nh-dh_table[i],nw-dw_table[i],board,depth+1); board[nh][nw] = 1; break; } } } } int main(){ int W,H; while(cin>>W>>H,W||H){ vector > board(H,vector(W)); int st_h,st_w; for(int h=0;h> board[h][w]; if(board[h][w] == 2){ board[h][w] = 0; st_h = h; st_w = w; }else if(board[h][w] == 3){ ed_h = h; ed_w = w; } } } min_path = INT_MAX/2; dfs(st_h,st_w,board,0); if(min_path == INT_MAX/2) cout << -1 << endl; else cout << min_path << endl; } }