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

  def initialize (given_ary)
    @ary = given_ary
  end

  def topface(val)
    4.times do
      self.roll("E")
      if self.top == val
        return
      end
    end
    3.times do
      self.roll("N")
      if self.top == val
        return
      end
    end
  end

  def frontface(val)
    4.times do
      tmp = @ary[1]
      @ary[1] = @ary[2]
      @ary[2] = @ary[4]
      @ary[4] = @ary[3]
      @ary[3] = tmp
     if @ary[1] == val
       return
     end
   end
 end

 def checkall(givendice)
   4.times do
     self.roll("E")
     4.times do
       tmp = @ary[1]
       @ary[1] = @ary[2]
       @ary[2] = @ary[4]
       @ary[4] = @ary[3]
       @ary[3] = tmp
      if identical(givendice)
        return true
      end
    end
  end
  3.times do
    self.roll("N")
    4.times do
      tmp = @ary[1]
      @ary[1] = @ary[2]
      @ary[2] = @ary[4]
      @ary[4] = @ary[3]
      @ary[3] = tmp
      if identical(givendice)
        return true
      end
    end
  end
  return false
end

def roll (direction)
case direction.downcase
when "e"
  tmp = @ary[1]
  @ary[0] = @ary[3]
  @ary[3] = @ary[5]
  @ary[5] = @ary[2]
  @ary[2] = tmp
when "w"
  tmp = @ary[1]
  @ary[0] = @ary[2]
  @ary[2] = @ary[5]
  @ary[5] = @ary[3]
  @ary[3] = tmp
when "s"
  tmp = @ary[1]
  @ary[0] = @ary[4]
  @ary[4] = @ary[5]
  @ary[5] = @ary[1]
  @ary[1] = tmp
when "n"
  tmp = @ary[1]
  @ary[0] = @ary[1]
  @ary[1] = @ary[5]
  @ary[5] = @ary[4]
  @ary[4] = tmp
 end
end

  def top
   @ary[0]
  end

  def front
    @ary[5]
  end

  def right
    @ary[2]
  end

  def vals
    @ary
  end

  def compare(givendice)
    4.times do
      self.roll("E")
      if identical(givendice)
        return true
      end
    end
    3.times do
      self.roll("N")
      if identical(givendice)
        return true
      end
      4.times do
        self.roll("E")
        if identical(givendice)
          return true
        end
      end

    end
    false

    end

  def identical(givendice)
    @ary.zip(givendice.vals).map{ |x,y| (x-y).abs}.inject(:+) == 0
  end

end

  dices = Array.new()
  gets.to_i.times do
    dices << Dice.new(gets.split.map(&:to_i))
end
  result = ""
  dices.each do |ouher|
    dices.each do |inner|
      if ouher === inner
        next
      end
      if ouher.checkall(inner)
        puts "No"
        exit
      end
    end
  end
 puts "Yes"