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

void throwdice_N(int x[6]){
    int tmp;
    tmp = x[0];
    x[0] = x[1];
    x[1] = x[5];
    x[5] = x[4];
    x[4] = tmp;
}
void throwdice_E(int x[6]){
    int tmp;
    tmp = x[0];
    x[0] = x[3];
    x[3] = x[5];
    x[5] = x[2];
    x[2] = tmp;
}
void throwdice_S(int x[6]){
    int tmp;
    tmp = x[0];
    x[0] = x[4];
    x[4] = x[5];
    x[5] = x[1];
    x[1] = tmp;
}
void throwdice_W(int x[6]){
    int tmp;
    tmp = x[0];
    x[0] = x[2];
    x[2] = x[5];
    x[5] = x[3];
    x[3] = tmp;
}

#include <stdio.h>
int main(int argc, const char * argv[]) {
    int i, j, top, front, left, right, back, bottom, count=0, count1=0, n=0;
    int top2, front2, left2, right2, back2, bottom2;
    int x[6], x2[6];
    
    scanf("%d", &n);
    
    for(i=0; i<6; i++) {
        scanf("%d",&x[i]);
    }
    back = x[4];
    top = x[0];
    right = x[2];
    left = x[3];
    bottom = x[5];
    front =x[1];
    
    for(j=0; j<n-1; j++){
        
        for(i=0; i<6; i++) {
            scanf("%d",&x2[i]);
        }
        back2 = x2[4];
        top2 = x2[0];
        right2 = x2[2];
        left2 = x2[3];
        bottom2 = x2[5];
        front2 =x2[1];
        
        while (x2[1] != front) {
            
            if((x2[1] != back) &&
               (x2[1] != top) &&
               (x2[1] != right) &&
               (x2[1] != left) &&
               (x2[1] != bottom) &&
               (x2[1] != front) ){
                count++;
                break;
            }
            
            if (x2[2] == front || x2[3] == front) {
                throwdice_E(x2);
            }
            throwdice_N(x2);
        }
        while(x2[5] != bottom){
            if(0 < count) break;
            throwdice_E(x2);
        }
        while (x2[0] != top) {
            if(0 < count) break;
            throwdice_E(x2);
        }
        
        back2 = x2[4];
        top2 = x2[0];
        right2 = x2[2];
        left2 = x2[3];
        bottom2 = x2[5];
        front2 =x2[1];
        
        if(back == back2 &&
           top == top2 &&
           right == right2 &&
           left == left2 &&
           bottom == bottom2 &&
           front == front2){
            count1++;
        }
    }
    if(count1 == 0 ){
        printf("Yes\n");
    }else{
        printf("No\n");
    }
    
    return 0;
}