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;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		
		int ary[][] = new int[num][6];
		
		for(int i=0; i<num; i++){
			for(int j=0; j<6; j++){
				ary[i][j] = sc.nextInt();
			}
		}
		int ary2[] = new int[6];
		int ary3[] = new int[6];
		int co = 0;
	it:	for(int i=0; i<num-1; i++){
			for(int j=i+1; j<num; j++){
				for(int k=0; k<6; k++){
					ary2[k]=ary[i][k];
					ary3[k]=ary[j][k];
				}
				co = plan(ary2,ary3);
				if(co==4)break it;
			}
		}
		if(co==4){
			System.out.println("No");
		}else{
			System.out.println("Yes");
		}
	}
		
		public static int plan(int[] d, int[] t){
		int sd1ary[] = {d[0], d[5], d[1], d[2], d[4], d[3]};
		int sd2ary[] = {t[0], t[1], t[2], t[3], t[4], t[5]};
		int sd3ary[][] = 
			   {{t[0], t[5], t[1], t[2], t[4], t[3]}, 
				{t[1], t[4], t[0], t[3], t[5], t[2]}, 
				{t[2], t[3], t[4], t[0], t[1], t[5]},  
				{t[3], t[2], t[4], t[5], t[1], t[0]},  
				{t[4], t[1], t[2], t[5], t[3], t[0]},  
				{t[5], t[0], t[3], t[4], t[2], t[1]}};
		int count = 0;
		int add = 0;
		
  out : for(int k=0; k<6; k++){
			if(sd1ary[0] == sd2ary[k]){
				if(sd1ary[2]==sd3ary[k][2])continue;
				for(int j=2; j<6; j++){
					count=0;
					if(sd1ary[2]==sd3ary[k][j]){
						for(int n=0; n<4; n++){
							add = j+n;
							if(add>5) add=add-4;

							if(sd1ary[n+2]==sd3ary[k][add]){
								count++;
								if(count==4)break out;
							}
						}
					}
				}
			}
		}
		return count;
	}
}