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)

#!/usr/bin/env ruby

def rotate(dice,direction)
    case direction
    when "N"
        return [dice[1],dice[5],dice[2],dice[3],dice[0],dice[4]]
    when "E"
        return [dice[3],dice[1],dice[0],dice[5],dice[4],dice[2]]
    when "W"
        return [dice[2],dice[1],dice[5],dice[0],dice[4],dice[3]]
    when "S"
        return [dice[4],dice[0],dice[2],dice[3],dice[5],dice[1]]
    when "Z"
        return [dice[0],dice[2],dice[4],dice[1],dice[3],dice[5]]
    end
end

def changetop(dice,i)
    case i
    when 0 
        return dice
    when 1 
        return rotate(dice,"N")
    when 2
        return rotate(dice,"W")
    when 3
        return rotate(dice,"E")
    when 4
        return rotate(dice,"S")
    when 5
        return rotate(rotate(dice,"N"),"N")
    end
end

def judge(diceA,diceB)
    6.times do |index|
        tempdice = changetop(diceB,index)
        if tempdice == diceA then
            return true
            exit
        end
        3.times do 
            tempdice = rotate(tempdice,"Z")
            if tempdice == diceA then
                return true
                exit
            end
        end
    end
    return false
end

loop_number = gets.to_i
dices = []
loop_number.times do
    dices << gets.split.map(&:to_i) 
end
dices = dices.combination(2).to_a

dices.count.times do |index|
    if judge(dices[index][0],dices[index][1]) == true then
        puts "No"
        exit
    end
end
puts "Yes"