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: Wrong Answer)

class Dice
  def initialize(numbers)
    @numbers = numbers
    @map = {:top => 1, :R => 6, :S => 2, :E => 3, :N => 5, :W => 4}
  end

  def move(vec)
    case vec
    when "N"
      temp = @map.clone
      temp[:top] = @map[:S]
      temp[:R] = @map[:N]
      temp[:S] = @map[:R]
      temp[:N] = @map[:top]
      @map = temp.clone
    when "W"
      temp = @map.clone
      temp[:top] = @map[:E]
      temp[:R] = @map[:W]
      temp[:E] = @map[:R]
      temp[:W] = @map[:top]
      @map = temp.clone
    when "S"
      temp = @map.clone
      temp[:top] = @map[:N]
      temp[:R] = @map[:S]
      temp[:S] = @map[:top]
      temp[:N] = @map[:R]
      @map = temp.clone
    when "E"
      temp = @map.clone
      temp[:top] = @map[:W]
      temp[:R] = @map[:E]
      temp[:E] = @map[:top]
      temp[:W] = @map[:R]
      @map = temp.clone
    end
  end

  def spin
    temp = @map.clone
    temp[:E] = @map[:N]
    temp[:W] = @map[:S]
    temp[:S] = @map[:E]
    temp[:N] = @map[:W]
    @map = temp.clone
  end

  def top_number
    @numbers[@map[:top] - 1]
  end

  def right_number
    @numbers[@map[:E] - 1]
  end

  def front_number
    @numbers[@map[:S] - 1]
  end

  def back_number
    @numbers[@map[:N] - 1]
  end

  def left_number
    @numbers[@map[:W] - 1]
  end

  def bottom_number
    @numbers[@map[:R] - 1]
  end

  def get_place(number)
    index = @numbers.index(number)
    @map.key(index + 1)
  end

  def numbers
    @numbers
  end

  def map
    @map
  end

  def equal?(dice)
    dice.numbers.each do |i|
      if @numbers.index(i) == nil
        puts 1
        return false
      end
    end
    top = self.top_number
    while dice.top_number != top do
      place = dice.get_place(top)
      if place == :W || place == :E
        dice.move("W")
      else
        dice.move("S")
      end
    end

    3.times do
      if dice.front_number == self.front_number then break end
      dice.spin
    end
    if dice.front_number != self.front_number
      puts 2
      return false
    end
    dice.map.each do |key, val|
      if dice.numbers[val - 1] != self.numbers[self.map[key] - 1]
        return false
      end
    end
    true
  end

end

dice1 = Dice.new(gets.split.map(&:to_i))
dice2 = Dice.new(gets.split.map(&:to_i))

if dice1.equal?(dice2)
  puts "Yes"
else
  puts "No"
end