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: Memory Limit Exceeded)

import java.util.Random;
import java.util.Scanner;

public class Main 
{	
	public static void main(String[] args)
	{
		Scanner scanner = new Scanner(System.in);

		int x = scanner.nextInt();
		
		int[][] n = new int[x][6];
		for(int i=0; i<x; i++)
		{
			for(int j=0; j<6; j++)
			{
				n[i][j] = scanner.nextInt();
			}
		}
		
		for(int l=0; l<x; l++)
		{
			for(int k=l+1; k<x; k++)
			{
				for(int i=0; i<99999; i++)
				{
					Random random = new Random();
					int d = random.nextInt(4);
						
					int[] buf = new int[6];
					for(int j=0; j<6; j++)
					{
						buf[j] = n[k][j];
					}
							
					switch(d)
					{
						case 0:
							n[k][0] = buf[4];
							n[k][1] = buf[0];
							n[k][5] = buf[1];
							n[k][4] = buf[5];
							break;
						case 1:
							n[k][0] = buf[1];
							n[k][1] = buf[5];
							n[k][5] = buf[4];
							n[k][4] = buf[0];
							break;
						case 2:
							n[k][0] = buf[2];
							n[k][2] = buf[5];
							n[k][5] = buf[3];
							n[k][3] = buf[0];
							break;
						case 3:
							n[k][0] = buf[3];
							n[k][3] = buf[5];
							n[k][5] = buf[2];
							n[k][2] = buf[0];
							break;
					}
					
					boolean is = true;
					for(int j=0; j<6; j++)
					{
						if(n[l][j] != n[k][j])
						{
							is = false;
							break;
						}
					}
					if(is)
					{
						System.out.println("No");
						return;
					}
				}
			}
		}
		
		System.out.println("Yes");
	}
}