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)

class Dice
  attr_accessor :face

  def rotate(dir)
    case dir
    when "N"
      @face[0],@face[1],@face[5],@face[4] = @face[1],@face[5],@face[4],@face[0]
    when "E"
      @face[0],@face[2],@face[5],@face[3] = @face[3],@face[0],@face[2],@face[5]
    when "S"
      @face[0],@face[1],@face[5],@face[4] = @face[4],@face[0],@face[1],@face[5]
    when "W"
      @face[0],@face[2],@face[5],@face[3] = @face[2],@face[5],@face[3],@face[0]
    when "cw" #ClockWise rotation
      self.rotate("E").rotate("N").rotate("W")
    end
    return self
  end

  def aline_dir(top, front)
    alined_dice = []
    self.face.each_with_index do |item, index|
      dice_clone = Marshal.load(Marshal.dump(self))
      if item == top
        case index
        when 1
          dice_clone.rotate("N")
        when 2
          dice_clone.rotate("W")
        when 3
          dice_clone.rotate("E")
        when 4
          dice_clone.rotate("S")
        when 5
          dice_clone.rotate("N").rotate("N")
        end
        (1..4).each do |i|
          dice_clone2 = Marshal.load(Marshal.dump(dice_clone))
          if dice_clone.face[i] == front
            case i
            when 2
              dice_clone2.rotate("cw")
            when 3
              dice_clone2.rotate("cw").rotate("cw").rotate("cw")
            when 4
              dice_clone2.rotate("cw").rotate("cw")
            end
            alined_dice.push(dice_clone2.face)
          end
        end
      end
    end
    return alined_dice
  end
  
  def same?(dice_alined)
    bool = false
    dice_alined.each do |item|
      bool = true if self.face.eql?(item)
    end
    return bool
  end
end

dices = []
n = gets.chomp.to_i
n.times do |i|
  dices.push(Dice.new)
  dices[i].face = gets.chomp.split.map(&:to_i)
end

i = 0
bool = false
n.times do |i|
  for j in i+1..n-1
    alined_dice = dices[j].aline_dir(dices[i].face[0], dices[i].face[1])
    bool = true if dices[i].same?(alined_dice)
  end
end
puts bool == true ? "No" : "Yes"