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 Ruby (judge verdict: Wrong Answer)

class Dice
	attr_reader :number
	
	def initialize(input)
		# top bottom N E S W
		@number = input

		@dice_number = [1, 6, 5, 3, 2, 4]
	end

	def rotate(command)
		swap_number = []

		case command
		when 'N'
			swap_number = [2, 1, 4]
			swap(swap_number)
		when 'E'
			swap_number = [3, 1, 5]
			swap(swap_number)
		when 'S'
			swap_number = [4, 1, 2]
			swap(swap_number)
		when 'W'
			swap_number = [5, 1, 3]
			swap(swap_number)
		end
	end

	def rotate_horizontal
		tmp = @dice_number[2]
		(2..4).each {|i| @dice_number[i] = @dice_number[i+1]}
		@dice_number[5] = tmp
	end

	def swap(number)
		number.map do |n|
			@dice_number[0], @dice_number[n] = @dice_number[n], @dice_number[0]
		end
	end

	def numbers
		@dice_number
	end
end

def rotateToTop(dice, position)
	case position
	when 0 then
	when 1 then
		dice.rotate('N')
	when 2 then
		dice.rotate('W')
	when 3 then
		dice.rotate('E')
	when 4 then
		dice.rotate('S')
	when 5 then
		2.times { dice.rotate('S') }
	end
end

n = gets.to_i

dice = []
n.times do |i|
	inputs = gets.split.map(&:to_i)
	dice << Dice.new(inputs)
end

difference_flag = 0
n.times do |i|
	dice1 = Marshal.load(Marshal.dump(dice[i].clone))
	(i..(n-1)).each do |j|
		next if i == j

		match_flag = false
		(0..5).each do |e|
			dice2 = Marshal.load(Marshal.dump(dice[j].clone))

			rotateToTop(dice2, e)

			num_of_rotation = 0
			while (num_of_rotation+=1) <= 4 do
				if dice1.numbers == dice2.numbers then
					match_flag = true
					break
				end
		
				dice2.rotate_horizontal
			end
	
			if match_flag then
					difference_flag += 1
					break
			end
		end
	end
end

if difference_flag == 0 then
	puts "Yes"
else
	puts "No"
end