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 JAVA (judge verdict: Wrong Answer)

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int d[];
        int cnt=2;
        d=new int[6];
        boolean flag=true;
        for(int i=0;i<6;i++){
            d[i]=sc.nextInt();
        }
        Dice D1=new Dice(d[0],d[1],d[2],d[3],d[4],d[5]);
        while(flag && cnt<n){
            for(int i=0;i<6;i++){
                d[i]=sc.nextInt();
            }
            Dice D=new Dice(d[0],d[1],d[2],d[3],d[4],d[5]);
            cnt++;
            flag=sameDice(D,D1);
        }
        if(flag) System.out.println("Yes");
        else System.out.println("No");
    }
    public static boolean sameDice(Dice D,Dice D1){
        if(D.d2==D1.d1 || D.d5==D1.d1 || D.d6==D1.d1){
            while(D.d1!=D1.d1) D.roll('N');
        }
        else if(D.d3==D1.d1) D.roll('W');
        else if(D.d4==D1.d1) D.roll('E');
        if(D.d2!=D1.d2){
            while(D.d2!=D1.d2) D.slide();
        }
        if(D.d3==D1.d3 && D.d4==D1.d4 && D.d5==D1.d5 && D.d6==D1.d6) return false;
        else return true;
    }
}

class Dice{
    int d1,d2,d3,d4,d5,d6,db;
    Dice(int a,int b,int c,int d,int e,int f){
        this.d1=a;
        this.d2=b;
        this.d3=c;
        this.d4=d;
        this.d5=e;
        this.d6=f;
    }
    void roll(char c){
        switch (c){
            case 'N':
                db=d1;
                d1=d2;
                d2=d6;
                d6=d5;
                d5=db;
                break;
            case 'S':
                db=d1;
                d1=d5;
                d5=d6;
                d6=d2;
                d2=db;
                break;
            case 'E':
                db=d1;
                d1=d4;
                d4=d6;
                d6=d3;
                d3=db;
                break;
            case 'W':
                db=d1;
                d1=d3;
                d3=d6;
                d6=d4;
                d4=db;
                break;
        }
    }
    void slide(){
        db=d2;
        d2=d3;
        d3=d5;
        d5=d4;
        d4=db;
    }
}