Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice III

Write a program which reads the two dices constructed in the same way as Dice I, and determines whether these two dices are identical. You can roll a dice in the same way as Dice I, and if all integers observed from the six directions are the same as that of another dice, these dices can be considered as identical.

Input

In the first line, six integers assigned to faces of a dice are given in ascending order of their corresponding labels.

In the second line, six integers assigned to faces of another dice are given in ascending order of their corresponding labels.

Output

Print "Yes" if two dices are identical, otherwise "No" in a line.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

Sample Input 1

1 2 3 4 5 6
6 2 4 3 5 1

Sample Output 1

Yes

Sample Input 2

1 2 3 4 5 6
6 5 4 3 2 1

Sample Output 2

No

Solution in Ruby (judge verdict: Accepted)

# ITP1_11_B: Dice3

class Dice
  Side = Struct.new :sides, :back

  TOP    = 0
  FRONT  = 1
  RIGHT  = 2
  LEFT   = 3
  BACK   = 4
  BOTTOM = 5

  def initialize(labels)
    @labels = labels
    @sides  = layout_map
  end

  def pip_of(side)
    @labels[side]
  end

  def sides_of(side)
    @sides[side].sides
  end

  def back_of(side)
    @sides[side].back
  end
 
  def rotate(p1, p2, p3, p4)
    l = @labels
    l[p1], l[p2], l[p3], l[p4] = l[p4], l[p1], l[p2], l[p3]
  end
 
  def roll_to(direction)
    rotate *case direction
    when 'N'
      [TOP, FRONT, BOTTOM, BACK]
    when 'E'
      [TOP, LEFT, BOTTOM, RIGHT]
    when 'S'
      [TOP, BACK, BOTTOM, FRONT]
    when 'W'
      [TOP, RIGHT, BOTTOM, LEFT]
    end
  end

  def each_side
    @labels.each_with_index
  end

  def same_as?(other)
    mytop   = self.pip_of TOP
    mysides = self.sides_of TOP
    myback  = self.back_of TOP

    other.each_side.any? do |label, id|
      next unless mytop == label && myback == other.back_of(id)
      other_sides = other.sides_of(id)
      mysides.size.times.any? do
        mysides == other_sides.rotate!(1)
      end
    end
  end

  private

    def layout_map()
      Hash.new.tap do |sides|
        sides[TOP]    = make_sides [FRONT, RIGHT, BACK, LEFT], BOTTOM
        sides[FRONT]  = make_sides [BOTTOM, RIGHT, TOP, LEFT], BACK
        sides[RIGHT]  = make_sides [BOTTOM, BACK, TOP, FRONT], LEFT
        sides[LEFT]   = make_sides [BOTTOM, FRONT, TOP, BACK], RIGHT
        sides[BACK]   = make_sides [TOP, RIGHT, BOTTOM, LEFT], FRONT
        sides[BOTTOM] = make_sides [BACK, RIGHT, FRONT, LEFT], TOP
      end
    end

    def make_sides(sides, back)
      Side.new sides.map{ |id| @labels[id] }, @labels[back]
    end
end

labels1 = gets.chomp.split.map &:to_i
labels2 = gets.chomp.split.map &:to_i

dice1 = Dice.new labels1
dice2 = Dice.new labels2
puts dice1.same_as?(dice2) ? 'Yes' : 'No'