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: Runtime Error)

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static String str;
	static Scanner scan = new Scanner(System.in);
	public static void main(String[] args) {
		int n = Integer.parseInt(scan.nextLine());
		//???????????????????????°???dice???????´?
		str = scan.nextLine();
		String[] dice1 = str.split(" ");
		str = scan.nextLine();
		String[] dice2 = str.split(" ");
		int i = 0;

		while(true){
			if(!(sort(dice1, dice2))){
				str = scan.nextLine();
				dice2 = str.split(" ");
				if(n <= i) break;
				i++;
			}else{
				System.out.println("No");
				return;
			}
		}
		
		System.out.println("Yes");
		
		/*
		for (int i = 0; i < n; i++) {
			if(!(sort(dice1, dice2))){
				str = scan.nextLine();
				dice2 = str.split(" ");
			}else{
				System.out.println("No");
				return;
			}
		}
		*/
		
		//System.out.println("Yes");
		
	}

	/**
	 * ?????????????????¢????????????????±???????????????????
	 * @param dice
	 * @param diceOrigin
	 * @return bool
	 */
	private static boolean sort(String[] dice, String[] diceOrigin){
		boolean bool = false;
		for(int i = 0; i < 4; i++){
			for(int j = 0; j < 4; j++){
				for (int h = 0; h < 4; h++) {
					String[] direction = new String[i+j+(h*3)];
					String[] dice2 = Arrays.copyOf(dice, dice.length);
					//??????????????¢?????????
					for (int k = 0; k < i; k++) {
						direction[k] = "N";
					}
					//?¨????????????¢?????????
					for (int l = 0; l < j; l++) {
						direction[i+l] = "E";
					}
					//?????????????????????
					for (int f = 0; f < h; f++) {
						int num = f * 3;
						direction[i+j+num] = "W";
						direction[i+j+(num+1)] = "S";
						direction[i+j+(num+2)] = "E";
					}
					
					//move????????????
					bool = move(dice2, direction, diceOrigin);
					if(bool){
						return true;
					}
				}
			}
		}
		return bool;
	}
	
	/**
	 * ??????????????????????????????????????????
	 * @param dice
	 * @param direction
	 * @param diceOrigin
	 * @return diceCheck
	 */
	private static boolean move(String[] dice, String[] direction, String[] diceOrigin) {
		for (int i = 0; i < direction.length; i++) {
			if(direction[i].equals("E")){
				str = dice[0];
				dice[0] = dice[3];
				dice[3] = dice[5];
				dice[5] = dice[2];
				dice[2] = str;
			}else if(direction[i].equals("W")){
				str = dice[0];
				dice[0] = dice[2];
				dice[2] = dice[5];
				dice[5] = dice[3];
				dice[3] = str;
			}else if(direction[i].equals("S")){
				str = dice[0];
				dice[0] = dice[4];
				dice[4] = dice[5];
				dice[5] = dice[1];
				dice[1] = str;
			}else if(direction[i].equals("N")){
				str = dice[0];
				dice[0] = dice[1];
				dice[1] = dice[5];
				dice[5] = dice[4];
				dice[4] = str;
			}
		}
		
		/*
		 * ????????????????????????????????¢??¨?????????????????????????????¢???????????????
		 * ??¢????????£???????????°true??????????????§????????°false?????????
		 */
		boolean diceCheck = true;
		for (int i = 0; i < diceOrigin.length; i++) {
			for (int j = 0; j < diceOrigin.length; j++) {
				if(!(diceOrigin[i].equals(dice[i]))){
					return false;
				}
			}
		}
		return diceCheck;	
	}
}