def dfs(map, ys, xs, yg, xg, n)
  return 11 if n > 10
  min = 11
  [[1, 0], [-1, 0], [0, 1], [0, -1]].each do |dy, dx|
    v, u = ys + dy, xs + dx
    next if v < 0 or v >= map.size or u < 0 or u >= map[0].size or map[v][u] == 1
    v, u = v + dy, u + dx until v < 0 or v >= map.size or u < 0 or u >= map[0].size or (v == yg && u == xg) or map[v][u] == 1
    return n if v == yg && u == xg
    next if v < 0 or v >= map.size or u < 0 or u >= map[0].size
    map[v][u] = 0
    ret = dfs(map, v - dy, u - dx, yg, xg, n + 1)
    min = ret if ret < min
    map[v][u] = 1
  end
  return min
end

loop do
  w, h = gets.split.map(&:to_i)
  break if w == 0
  map = (1..h).map { gets.split.map(&:to_i) }
  start = goal = nil
  (0..h-1).each do |i|
    (0..w-1).each do |j|
      start = [i, j] if map[i][j] == 2
      goal = [i, j] if map[i][j] == 3
      map[i][j] = 0 unless map[i][j] == 1
    end
  end
  k = dfs(map, *start, *goal, 1)
  p k == 11 ? -1 : k
end

