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 Scala (judge verdict: Accepted)

object Main {
  def main(args: Array[String]): Unit = {
    val first  = Dice(io.StdIn.readLine().split(" ").map(_.toInt))
    val second = Dice(io.StdIn.readLine().split(" ").map(_.toInt))

    val answer =
      if (Dice.equals(first, second)) "Yes"
      else "No"

    println(answer)
  }

  case class Dice(upSide: Int, southSide: Int, eastSide: Int, westSide: Int, northSide: Int, downSide: Int) {
    import Direction._
    def spinClockwise: Dice = Dice(upSide, eastSide, northSide, southSide, westSide, downSide)
    def spinCounterclockwise: Dice = Dice(upSide, westSide, southSide, northSide, eastSide, downSide)
    def roll(direction: Direction): Dice = direction match {
      case North =>
        Dice(southSide, downSide, eastSide, westSide, upSide, northSide)
      case South =>
        Dice(northSide, upSide, eastSide, westSide, downSide, southSide)
      case West =>
        Dice(eastSide, southSide, downSide, upSide, northSide, westSide)
      case East =>
        Dice(westSide, southSide, upSide, downSide, northSide, eastSide)
    }
  }
  object Dice {
    def apply(sides: Array[Int]): Dice = Dice(sides(0), sides(1), sides(2), sides(3), sides(4), sides(5))

    def equals(d1: Dice, d2: Dice): Boolean = {
      import Direction._

      val sides = d1.northSide :: d1.westSide :: d1.southSide :: d1.eastSide ::
                  d1.northSide :: d1.westSide :: d1.southSide :: Nil
      @annotation.tailrec
      def sameDice(rolled: Option[Direction], d: Dice): Boolean = {
        val same =
          if (d1.upSide == d.upSide && d1.downSide == d.downSide) {
            sides.containsSlice(d.northSide :: d.westSide :: d.southSide :: d.eastSide :: Nil)
          } else if (d1.upSide == d.downSide && d1.downSide == d.upSide) {
            sides.containsSlice(d.northSide :: d.eastSide :: d.southSide :: d.westSide :: Nil)
          } else {
            false
          }
        if (same) {
          true
        } else {
          rolled match {
            case Some(North) => sameDice(Some(West), d.roll(North))
            case Some(West) => sameDice(None, d.roll(West))
            case _ => false
          }
        }
      }

      sameDice(Some(North), d2)
    }
  }

  sealed trait Direction
  object Direction {
    case object North extends Direction
    case object South extends Direction
    case object West extends Direction
    case object East extends Direction

    def apply(c: Char): Direction = c match {
      case 'N' => North
      case 'S' => South
      case 'E' => East
      case 'W' => West
      case _ => throw new Exception
    }
  }
}