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.Scanner;

class Main{
        public static void main(String[] args){
    	Scanner scan = new Scanner(System.in);
    	Dice[] dice;
    	boolean flag = true;
    	
    	int dice_num = scan.nextInt();
    	dice = new Dice[dice_num];
    	
    	for(int i = 0; i < dice_num; i++){
    		dice[i] = new Dice();
    	}
    	
    	for(int i = 0; i < dice_num; i++){
    		for(int j = 0; j < 6; j++){
    			dice[i].S[j] = scan.nextInt();
    		}
    	}
    	
    	for(int n = 0; n < dice_num - 1; n++){
    		for(int m = n + 1; m < dice_num; m++){
				for(int i = 0; i < 4; i++){
					for(int j = 0; j < 4; j++){
			    		for(int k = 0; k < 4; k++){
			    			int temp3 = dice[m-1].S[0];
			        		dice[m-1].S[0] = dice[m-1].S[1];
			        		dice[m-1].S[1] = dice[m-1].S[5];
			        		dice[m-1].S[5] = dice[m-1].S[4];
			        		dice[m-1].S[4] = temp3;
	    					if(dice[n].S[0] == dice[m].S[0] && dice[n].S[1] == dice[m].S[1] && dice[n].S[2] == dice[m].S[2] && dice[n].S[3] == dice[m].S[3] && dice[n].S[4] == dice[m].S[4] && dice[n].S[5] == dice[m].S[5]) {
	    						System.out.println("Yes");
	    						flag = false;
	    					}
			        	}
		    		}
		    		int temp2 = dice[m-1].S[0];
		    		dice[m-1].S[0] = dice[m-1].S[3];
		    		dice[m-1].S[3] = dice[m-1].S[5];
		    		dice[m-1].S[5] = dice[m-1].S[2];
		    		dice[m-1].S[2] = temp2;
				}
				int temp = dice[m-1].S[0];
				dice[m-1].S[0] = dice[m-1].S[1];
				dice[m-1].S[1] = dice[m-1].S[5];
				dice[m-1].S[5] = dice[m-1].S[4];
				dice[m-1].S[4] = temp;
    		}
    		if(flag == false) return;
		}
    	System.out.println("Yes");
     }
}

class Dice{
	int[] S = new int[6];
}