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| c[y][..] = c[y - 1] x = 1 - y % 2 while x < n - 1 cd0 = (pos[c[y - 1][x]] - x).abs cd1 = (pos[c[y - 1][x + 1]] - x - 1).abs nd0 = (pos[c[y - 1][x]] - x - 1).abs nd1 = (pos[c[y - 1][x + 1]] - x).abs if cd0 ** 2 + cd1 ** 2 > nd0 ** 2 + nd1 ** 2 c[y][x] = c[y - 1][x + 1] c[y][x + 1] = c[y - 1][x] end x += 2 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