def reflect(x, y, w, h, dx, dy, board)
  c = 0
  loop do
    x += dx; y += dy
    break if 0 > x || x >= w || 0 > y || y >= h
    if board[y][x] == 1
      return c
    elsif board[y][x] == 3
      return -1
    end
    c += 1
  end
end

loop do
  w, h = gets.split.map(&:to_i)
  break if w == 0 && h == 0
  sx, sy = 0, 0
  board = h.times.map{ |y|
    row = gets.split(' ').map(&:to_i)
    if x = row.index(2) then sx, sy = x, y end
    row
  }
  pos = []
  pos << [sx, sy, 1, board]
  ans = -1
  until pos.empty?
    x, y, i, b = pos.shift
    [[-1, 0], [0, 1], [1, 0], [0, -1]].each do |dx, dy|
      if len = reflect(x, y, w, h, dx, dy, b)
        if len == -1 # goal
          ans = i
          break
        elsif i < 10 && len != 0
          xx = len * dx + x; yy = len * dy + y
          nb = b.map(&:dup)
          nb[yy + dy][xx + dx] = 0
          pos << [xx, yy, i + 1, nb]
        end
      end
    end
    break if ans != -1
  end
  puts ans
end