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

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        

        int N = Integer.parseInt(sc.nextLine());

        String[] a = null;
        int[] men = null;
        ArrayList<Dice> dices = new ArrayList<>();
        for (int j = 0; j < N; j++) {
            
            a = sc.nextLine().split(" ");
            men = new int[a.length];
            for (int i = 0; i < a.length; i++) {
                men[i] = Integer.parseInt(a[i]);
            }

            dices.add(new Dice(men));
        }

        for (int i = 0; i < dices.size(); i++) {
            for (int j = 0; j < dices.size(); j++) {
                if (i==j) { continue; }
                
                Dice dice1 = dices.get(i);
                Dice dice2 = dices.get(j);

                // 1. top = default, bottom = default
                dice1.checkEqual(dice2);

                // 2. reverse 1.
                dice2.turnN(); dice2.turnN();
                dice1.checkEqual(dice2);
                dice2.turnN(); dice2.turnN();  // back to 1.

                // 3. top = front, bottom = back
                dice2.turnN(); 
                dice1.checkEqual(dice2);

                // 4. reverse 3.
                dice2.turnN(); dice2.turnN();
                dice1.checkEqual(dice2);
                dice2.turnN(); // back to 1.

                // 5. top = left, bottom = right
                dice2.turnE();
                dice1.checkEqual(dice2);

                // 6. reverse 5.
                dice2.turnE(); dice2.turnE();
                dice1.checkEqual(dice2);
                dice2.turnE(); // back to 1. 
                
            }
    
        }

        System.out.println("Yes");
    }
}

class Dice {
    int top, front, right, left, back, bottom;

    public Dice(int[] men){
        this.top = men[0];
        this.front = men[1];
        this.right = men[2];
        this.left = men[3];
        this.back = men[4];
        this.bottom = men[5];
    }

    void checkEqual(Dice dice) {
        if ( this.equals(dice) ) {
            System.out.println("No");
            System.exit(0);
        } else {
            // nothing
        }
    }

    boolean equals(Dice dice) {
        if (this.top == dice.top) {
            if (this.bottom == dice.bottom) {
                for (int i=0; i<4; i++) {
                    if (this.right == dice.right 
                        && this.left == dice.left 
                        && this.back == dice.back 
                        && this.front == dice.front ) {
                        
                        return true;
                    } else {
                        this.turnR();
                    }
                }
            }
        }

        return false;
    }

    void turnN() {
        int tmp = this.top;
        this.top = this.front;
        this.front = this.bottom;
        this.bottom = this.back;
        this.back = tmp;
    }
    void turnS() {
        this.turnN();
        this.turnN();
        this.turnN();
    }
    void turnE() {
        int tmp = this.top;
        this.top = this.left;
        this.left = this.bottom;
        this.bottom = this.right;
        this.right = tmp;
    }
    void turnW() {
        this.turnE();
        this.turnE();
        this.turnE();
    }
    void turnR() {
        int tmp = this.front;
        this.front = this.right;
        this.right = this.back;
        this.back = this.left;
        this.left = tmp;
    }
    void turnL() {
        this.turnR();
        this.turnR();
        this.turnR();
    }
}