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: Time Limit Exceeded)

import Data.List

toDice :: [Int] -> [[Int]]
toDice [a,b,c,d,e,f] = [[a,f],[b,e],[c,d]]

pattern1 :: [Int] -> [String]
pattern1 [a,b,_,_,e,f] = map show [a,b,f,e,a]

pattern2 :: [Int] -> [String]
pattern2 [a,_,c,d,_,f] = map show [c,a,d,f,c]

pattern3 :: [Int] -> [String]
pattern3 [_,b,c,d,e,_] = map show [b,c,e,d,b]

solve :: [Int] -> String -> Int
solve list x
  | isInfixOf x (unwords $ pattern1 list) = list !! 2
  | isInfixOf x (unwords $ pattern2 list) = list !! 1
  | isInfixOf x (unwords $ pattern3 list) = list !! 0
  | isInfixOf x (unwords $ reverse $ pattern1 list) = list !! 3
  | isInfixOf x (unwords $ reverse $ pattern2 list) = list !! 4
  | isInfixOf x (unwords $ reverse $ pattern3 list) = list !! 5
  | otherwise = -1                                                           

check1 :: [Int] -> [Int] -> Bool
check1 xs ys = (solve' xs) == (solve' ys)
  where solve' = sort . map sort . toDice

check2 :: [Int] -> [Int] -> String -> Bool
check2 xs ys s = (solve xs s) == (solve ys s)

diff :: [Int] -> [Int] -> Bool
diff xs ys = (check1 xs ys) && (check2 xs ys (mkstr xs))
  where mkstr = unwords . map show . take 2

main = getContents >>= putStrLn . (\b -> if or b then "No" else "Yes") . (\((n:_):xs) -> [ diff (xs !! a) (xs !! b) | [a,b] <- (conbination n)]) . read' . lines
  where read' = map (map read . words)
        conbination n = sort $ nub $ map (sort . take 2) $ permutations [0..n-1]