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

class Dice:
    def __init__(self, men):
        self.men = men

    def throw(self, direction):
        if direction == "E":
            pmen = men[:]
            men[0] = pmen[3]
            men[1] = pmen[1]
            men[2] = pmen[0]
            men[3] = pmen[5]
            men[4] = pmen[4]
            men[5] = pmen[2]
        elif direction == "W":
            pmen = men[:]
            men[0] = pmen[2]
            men[1] = pmen[1]
            men[2] = pmen[5]
            men[3] = pmen[0]
            men[4] = pmen[4]
            men[5] = pmen[3]
        elif direction == "S":
            pmen = men[:]
            men[0] = pmen[4]
            men[1] = pmen[0]
            men[2] = pmen[2]
            men[3] = pmen[3]
            men[4] = pmen[5]
            men[5] = pmen[1]
        elif direction == "N":
            pmen = men[:]
            men[0] = pmen[1]
            men[1] = pmen[5]
            men[2] = pmen[2]
            men[3] = pmen[3]
            men[4] = pmen[0]
            men[5] = pmen[4]
        elif direction == "C":
            pmen = men[:]
            men[0] = pmen[0]
            men[1] = pmen[2]
            men[2] = pmen[4]
            men[3] = pmen[1]
            men[4] = pmen[3]
            men[5] = pmen[5]
        elif direction == "U":
            pmen = men[:]
            men[0] = pmen[0]
            men[1] = pmen[3]
            men[2] = pmen[1]
            men[3] = pmen[4]
            men[4] = pmen[2]
            men[5] = pmen[5]

    def makeUe(self, ue):
        if(self.men[2] == ue or self.men[3] == ue):
            self.throw("E")

        while (self.men[0] != ue):
            self.throw("S")

n = int(raw_input())
anti2 = []
antiNot2 = []
for i in range(n):
    men = map(int, (raw_input()).split(" "))
    d = Dice(men)
    d.makeUe(1)
    if d.men[5] == 2:
        while (d.men[1] != 3):
            d.throw("C")
        S = d.men[2:5]
        anti2.append(S)
    else:
        while (d.men[1] != 2):
            d.throw("C")
        S = d.men[2:6]
        antiNot2.append(S)

flag = False
for i in range(len(anti2)-1):
    if anti2[i] == anti2[i+1]:
        flag = True

for i in range(len(antiNot2)-1):
    if antiNot2[i] == antiNot2[i+1]:
        flag = True

if flag:
    print "No"
else:
    print "Yes"