n = read_line.to_i a = read_line.split.map(&.to_i) b = read_line.split.map(&.to_i) pos = Array.new(n + 1, 0) n.times do |i| pos[b[i]] = i end c = Array.new(n + 1) { Array.new(n, 0) } c[0][..] = a c[-1][..] = b 1.upto(n - 1) do |y| x = 0 while x < n if x == n - 1 c[y][x] = c[y - 1][x] break end if (x <= pos[c[y - 1][x]] && x + 1 > pos[c[y - 1][x + 1]]) || (x < pos[c[y - 1][x]] && x + 1 >= pos[c[y - 1][x + 1]]) c[y][x] = c[y - 1][x + 1] c[y][x + 1] = c[y - 1][x] x += 2 else c[y][x] = c[y - 1][x] x += 1 end end end if n.times.all? { |i| (pos[c[n - 1][i]] - i).abs <= 1 } c.each { |r| puts r.join(" ") } else puts -1 end