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

Dice IV

Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III.

Input

In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III.

Output

Print "Yes" if given dices are all different, otherwise "No" in a line.

Constraints

$2 \leq n \leq 100$

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

Sample Input 1

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

Sample Output 1

No

Sample Input 2

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

Sample Output 2

Yes

Solution in Scala (judge verdict: Accepted)

object Main {
  def main(args: Array[String]): Unit = {
    val scanner = new java.util.Scanner(System.in)

    val n = scanner.nextInt
    val t1, s1, e1, w1, n1, b1 = scanner.nextInt
    var d1 = Dice(t1, n1, e1, s1, w1, b1)

    for(_ <- 2 to n) {
      val t2, s2, e2, w2, n2, b2 = scanner.nextInt
      var d2 = Dice(t2, n2, e2, s2, w2, b2)

      for(_ <- 1 to 4) {
        for(_ <- 1 to 4) {
          for(_ <- 1 to 4) {
            for(_ <- 1 to 4) {
              if(d1.equals(d2)) {
                println("No")
                System.exit(0)
              }
              d2 = d2.rotN()
            }
            d2 = d2.rotE()
          }
          d1 = d1.rotN()
        }
        d1 = d1.rotE()
      }
    }

    println("Yes")
  }
}

case class Dice(t: Int, n: Int, e: Int, s: Int, w: Int, b: Int) {
  def rotN(): Dice = Dice(s, t, e, b, w, n)
  def rotS(): Dice = Dice(n, b, e, t, w, s)
  def rotW(): Dice = Dice(e, n, b, s, t, w)
  def rotE(): Dice = Dice(w, n, t, s, b, e)
  def rot(): Dice = Dice(t, e, s, w, n, b)
  def equals(that: Dice): Boolean = (t == that.t && n == that.n && e == that.e && s == that.s && w == that.w && b == that.b)
}