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

def vertical_roll(_new_dice, _upper_value)
  _count = 0
  while _new_dice[0] != _upper_value && _count < 4
    _new_dice[0], _new_dice[1], _new_dice[4], _new_dice[5] = _new_dice[1], _new_dice[5], _new_dice[0], _new_dice[4]
  end
  _new_dice
end

def horizontal_roll(_new_dice, _front_value)
  _count = 0
  while _new_dice[1] != _front_value && _count < 4
    _new_dice[1], _new_dice[2], _new_dice[4], _new_dice[3] = _new_dice[2], _new_dice[4], _new_dice[3], _new_dice[1]
    _count += 1
  end
  _new_dice
end

def same_judge(_dice_a, _dice_b)
  if _dice_a == _dice_b
    puts "No"
    exit
  end
end

n = gets(chomp:true).to_i

_dice_a = gets.split.map(&:to_i)
_dice_table = [].push(_dice_a.join.to_i)

(n-1).times do
  _dice_b = gets.split.map(&:to_i)

  same_judge(_dice_a, _dice_b)

  _dice_b_idx = _dice_b.index(_dice_a[0])

  case _dice_b_idx
  when 0, 1, 5, 4
    vertical_roll(_dice_b, _dice_a[0])
    horizontal_roll(_dice_b, _dice_a[1])
  else
    horizontal_roll(_dice_b, _dice_a[0])
    vertical_roll(_dice_b, _dice_a[0])
    horizontal_roll(_dice_b, _dice_a[1])
  end
  
  same_judge(_dice_a, _dice_b)

  _dice_table << _dice_b.join.to_i
  _dice_a = _dice_b
end

if _dice_table.size == _dice_table.uniq.size
  puts "Yes"
else
  puts "No"
end