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 C# (judge verdict: Time Limit Exceeded)

using LIB;
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;

class Program
{
    public class dice
    {
        public int n, e, w, s, t, b;
        private int ni, ei, wi, si, ti, bi;
        public dice(int t, int s, int e, int w, int n, int b)
        {
            this.n = n;
            this.e = e;
            this.w = w;
            this.s = s;
            this.t = t;
            this.b = b;
            ni = n;
            ei = e;
            wi = w;
            si = s;
            ti = t;
            bi = b;
        }
        public void init()
        {
            n = ni;
            e = ei;
            w = wi;
            s = si;
            t = ti;
            b = bi;
        }
        public void rotateCW()
        {
            int buf = s;
            s = e;
            e = n;
            n = w;
            w = buf;
        }
        public bool same(dice dice)
        {

            bool f = false;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    init();
                    for (int k = 0; k < i; k++) { rotateCW(); }
                    for (int k = 0; k < j; k++) { roll('N'); }
                    while (true)
                    {
                        if (t == dice.t) { break; }
                        roll('N');
                        if (t == dice.t) { break; }
                        roll('E');
                    }
                    for (int k = 0; k < 4; k++)
                    {
                        if (s == dice.s) { break; }
                        rotateCW();
                    }
                    if ((n == dice.n && e == dice.e && w == dice.w && s == dice.s && t == dice.t && b == dice.b)) { f = true; break; }
                }
            }
            return f;
        }
        public void roll(char r)
        {
            int buf;
            switch (r)
            {
                case 'N':
                    buf = t;
                    t = s;
                    s = b;
                    b = n;
                    n = buf;
                    break;

                case 'E':
                    buf = t;
                    t = w;
                    w = b;
                    b = e;
                    e = buf;
                    break;

                case 'W':
                    buf = t;
                    t = e;
                    e = b;
                    b = w;
                    w = buf;
                    break;

                case 'S':
                    buf = t;
                    t = n;
                    n = b;
                    b = s;
                    s = buf;
                    break;
            }
        }
    }
    static void Main(string[] args)
    {
        int n = io.r<int>();
        int[][] d = io.r<int>(n, ' ');
        dice[] dice = new dice[n];
        bool f = true;
        for (int i = 0; i < n; i++)
        {
            dice[i] = new dice(d[i][0], d[i][1], d[i][2], d[i][3], d[i][4], d[i][5]);
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                if (dice[i].same(dice[j])) { f = false; break; }
            }
            if (!f) { break; }
        }
        if (f) { io.w("Yes"); } else { io.w("No"); }
        io.wflush();
    }
}

namespace LIB
{
    public class io
    {
        private const int WMAX = 1000;
        private static StringBuilder S = new StringBuilder();
        public static T r<T>() { return util.parse<T>(r()); }
        public static T[] r<T>(char s = ' ') { return r().Split(s).Select(util.parse<T>).ToArray(); }
        public static T[] r<T>(int l) { T[] r = new T[l]; for (int i = 0; i < l; i++) { r[i] = r<T>(); } return r; }
        public static T[][] r<T>(int l, char s = ' ') { T[][] r = new T[l][]; for (int i = 0; i < l; i++) { r[i] = r<T>(s); } return r; }
        private static string r() { return Console.ReadLine(); }
        public static void w(object v, bool lf = true) { S.Append(util.parse<string>(v)); if (lf == true) { S.Append('\n'); } if (S.Length >= WMAX) { wflush(); } }
        public static void wflush() { Console.Write(S.ToString()); S.Length = 0; }
    }
    public class util
    {
        public static T parse<T>(object value) { return (T)(Convert.ChangeType(value, typeof(T))); }
    }
    public class memo<Key, Result>
    {
        private Dictionary<Key, Result> R;
        public memo() { R = new Dictionary<Key, Result>(); }
        public Result exec(Key k, Func<Key, Result> f) { Result r; if (R.ContainsKey(k)) { r = R[k]; } else { r = f(k); R.Add(k, r); } return r; }
    }
}