L = 10
table = [[[0, 0], [0, 1], [-1, 1], [1, 1], [0, 2], [-1, 2], [-2, 2], [1, 2],
  [2, 2], [0, 3], [-1, 3], [1, 3], [0, 4]],
  [[0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [2, 1], [0, 2], [1, 2], [2, 2]],
  [[0, 0], [0, 1], [-1, 1], [1, 1], [0, 2]]]

n = $<.gets.to_i
cloth = $<.readlines.map {|l| l.split.map(&:to_i)}

copy = ->(ary) { ary.map {|l| l.dup} }
form = ->(x, y, i) {
  case (j = 3 - i)
  when 1 then "#{x} #{y + 1} #{j}"
  when 2 then "#{x + 1} #{y + 1} #{j}"
  when 3 then "#{x} #{y + 2} #{j}"
  else nil
  end
}

try = ->(place, field, co, order = []) {
  x = y = nil
  loop do
    return false if place >= L * L
    x, y = place % L, place / L
    break if field[y][x].nonzero?
    place += 1
  end
  
  result = false
  table.each_with_index do |pat, i|
    tmp = copy.(field)
    catch(:jump) do
      pat.each do |po|
        x1, y1 = x + po[0], y + po[1]
        throw(:jump) if x1 < 0 or x1 >= L or y1 >= L or tmp[y1][x1].zero?
        tmp[y1][x1] -= 1
      end
      co -= 1
      prc = form.(x, y, i)
      if co.zero?
        if tmp.flatten.map(&:zero?).all?
          return order + [prc]
        else
          co += 1
          throw(:jump)
        end
      end
      result = try.(place, tmp, co, order + [prc])
      return result if result
      co += 1
    end
  end
  result
}
puts try.(0, cloth, n)
