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: Accepted)

# frozen_string_literal: true

class Dice
  def initialize(params)
    @v = Array.new(6)
    (0..5).each do |i|
      @v[i] = params[i]
    end
    @phases = []
  end

  def faces
    @v
  end

  def slide(side_a, side_b, side_c, side_d)
    tmp = @v[side_a]
    @v[side_a] = @v[side_b]
    @v[side_b] = @v[side_c]
    @v[side_c] = @v[side_d]
    @v[side_d] = tmp
  end

  def rotate_atom(char)
    case char
    when 'N' then slide(0, 1, 5, 4)
    when 'E' then slide(0, 3, 5, 2)
    when 'W' then slide(0, 2, 5, 3)
    when 'S' then slide(0, 4, 5, 1)
    when 'D' then slide(1, 3, 4, 2)
    when 'U' then slide(1, 2, 4, 3)
    end
  end

  def rotate(string)
    string.chars.each do |c|
      rotate_atom(c)
    end
  end

  def top
    @v[0]
  end

  def register_phase
    4.times do
      @phases << Marshal.load(Marshal.dump(@v))
      rotate_atom('D')
    end
  end

  def register
    4.times do
      register_phase
      rotate_atom('S')
    end
    rotate_atom('W')
    register_phase
    rotate_atom('E')
    rotate_atom('E')
    register_phase
    rotate_atom('W')
  end

  def same_dice?(dice_y)
    register if @phases.empty?
    if dice_y.is_a?(Array)
      @phases.include?(dice_y)
    elsif dice_y.is_a?(Dice)
      @phases.include?(dice_y.faces)
    end
  end
end

n = gets.to_i
dices = n.times.map { Dice.new(readline.split(' ')) }
unique = (1...n).all? do |i|
  (0...i).all? { |j| !dices[i].same_dice?(dices[j]) }
end
puts unique ? 'Yes' : 'No'