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 Python3 (judge verdict: Time Limit Exceeded)

import random
class Dice:
    def __init__(self, num_list):
        self.top = num_list[0]
        self.dside = num_list[1]
        self.rside = num_list[2]
        self.lside = num_list[3]
        self.uside = num_list[4]
        self.bottom = num_list[5]
    def north(self):
        (self.top, self.dside, self.bottom, self.uside) = (self.dside, 
            self.bottom, self.uside, self.top)
    def east(self):
        (self.top, self.lside, self.bottom, self.rside) = (self.lside,
            self.bottom, self.rside, self.top)
    def south(self):
        (self.top, self.dside, self.bottom, self.uside) = (self.uside, 
            self.top, self.dside, self.bottom)

n = int(input())
num = [list(map(int, input().split())) for i in range(n)]
dice = [Dice(num[i]) for i in range(n)]
for idx1, i in enumerate(dice):
    for idx2, j in enumerate(dice):
        if idx2 <= idx1:
            continue
        elif len(set(num[0])) < 4:
            while(i.top != j.top or i.rside != j.rside
                  or i.dside != j.dside):
                mode = random.randint(0, 2)
                if mode==0:
                    j.north()
                elif mode==1:
                    j.east()
                else:
                    j.south()
        else:
            while i.top != j.top or i.rside != j.rside:
                mode = random.randint(0, 2)
                if mode==0:
                    j.north()
                elif mode==1:
                    j.east()
                else:
                    j.south()
        if (i.bottom == j.bottom and i.lside == j.lside 
            and i.dside == j.dside):
            print("No")
            break
    else:
        continue
    break
else:
    print("Yes")