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 Haskell (judge verdict: Accepted)

import           Control.Applicative
import           Control.Monad
import           Data.List

type Dice = [Int]

leq :: Dice -> Dice -> Bool
leq [] [] = True
leq (x:xs) (y:ys)
    | x < y = True
    | x > y = False
    | otherwise = leq xs ys

minDice:: Dice -> Dice -> Dice
minDice d1 d2
    | leq d1 d2 = d1
    | otherwise = d2

rotate:: Dice -> Char -> Dice
rotate [u,f,r,l,b,d] 'E' = [l,f,u,d,b,r]
rotate [u,f,r,l,b,d] 'N' = [f,d,r,l,u,b]
rotate [u,f,r,l,b,d] 'S' = [b,u,r,l,d,f]
rotate [u,f,r,l,b,d] 'W' = [r,f,d,u,b,l]
rotate xs _              = xs

search :: [Dice]->[Dice]->Dice
search dices searched
  | null dices =  foldl1 minDice searched
  | otherwise = search next (searched ++ dices)
  where
    next = filter (`notElem` searched) [rotate d opt | d <-dices, opt<-"ENSW" ]

main :: IO ()
main = do
  num <- getLine
  dices <- replicateM (read num) (map (read::String->Int) . words <$> getLine)
  let ds = [search [d] [] | d <-dices]
  putStrLn $ if length ds == length (nub ds) then "Yes" else "No"