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: Accepted)

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a=scan.nextInt();
        Dice[] dice=new Dice[a];
        int ans=-1;;
        int i=0,j=0;
        for(i=0;i<a;i++) {
        dice[i]=new Dice(scan.nextInt(),scan.nextInt(),scan.nextInt(),scan.nextInt(),scan.nextInt(),scan.nextInt());
        }
        for(i=0;i<a;i++) {
        	for(j=1;j<a;j++) {
        		if(i != j) {
        		ans=dice[i].ans(dice[i], dice[j]);
        		}
        		if(ans==0) {
        			System.out.println("No");
        			break;
        		}
        	}
        	if(ans==0) {
        		break;
        	}
        }
        if(ans==1) {
        	System.out.println("Yes");
        }
        
        scan.close();
    }
}

class Dice{
	int Top;	//1
	int S;		//2
	int N;		//5
	int W;		//4
	int E;		//3
	int Bo;		//6
	int tmp;
	
	Dice(int a,int b,int c,int d,int e,int f){
		Top=a;
		S=b;
		N=e;
		W=d;
		E=c;
		Bo=f;
	}
	
	void TrunS() {
		tmp=Top;
		Top=N;
		N=Bo;
		Bo=S;
		S=tmp;
	}void TrunN() {
		tmp=Top;
		Top=S;
		S=Bo;
		Bo=N;
		N=tmp;
	}void TrunW() {
		tmp=Top;
		Top=E;
		E=Bo;
		Bo=W;
		W=tmp;
	}void TrunE() {
		tmp=Top;
		Top=W;
		W=Bo;
		Bo=E;
		E=tmp;				//問題発生　723546  723586
	}
	int ans(Dice dice,Dice dice2) {		//DICEⅢの課題に対するメソッド
		int x=1;
		int count=0;
		if(dice.Top==dice2.W) {
			dice2.TrunE();
		}else if(dice.Top==dice2.N) {
			dice2.TrunS();
		}else if(dice.Top==dice2.E) {
			dice2.TrunW();
		}else if(dice.Top==dice2.S) {
			dice2.TrunN();
		}else if(dice.Top==dice2.Bo) {
			dice2.TrunS();
			dice2.TrunS();
		}else if(dice.Top==dice2.Top) {
			//何もしない
		}else {
			return x;
		}//ここでTop調整終了
		if(dice.Bo==dice2.Bo) {
			if(dice.S==dice2.N) {
				if(dice.E==dice2.W && dice.W==dice2.E) {
					x=0;
				}
			}else if(dice.S==dice2.S) {
				if(dice.E==dice2.E && dice.W==dice2.W && dice.N==dice2.N) {
					x=0;
				}
			}else if(dice.S==dice2.E) {
				if(dice.E==dice2.N && dice.W==dice2.S) {
					x=0;
				}
			}else if(dice.S==dice2.W) {
				if(dice.E==dice2.S && dice.W==dice2.N) {
					x=0;
					
				}
			}
			if(dice.Top==dice2.Top) {
				count++;
			}if(dice.S==dice2.S) {
				count++;
			}if(dice.W==dice2.W) {
				count++;
			}if(dice.N==dice2.N) {
				count++;
			}if(dice.E==dice2.E) {
				count++;
			}if(count ==5) {
				x=0;
			}
		}
	return x;
	}
}