Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice III

Write a program which reads the two dices constructed in the same way as Dice I, and determines whether these two dices are identical. You can roll a dice in the same way as Dice I, and if all integers observed from the six directions are the same as that of another dice, these dices can be considered as identical.

Input

In the first line, six integers assigned to faces of a dice are given in ascending order of their corresponding labels.

In the second line, six integers assigned to faces of another dice are given in ascending order of their corresponding labels.

Output

Print "Yes" if two dices are identical, otherwise "No" in a line.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

Sample Input 1

1 2 3 4 5 6
6 2 4 3 5 1

Sample Output 1

Yes

Sample Input 2

1 2 3 4 5 6
6 5 4 3 2 1

Sample Output 2

No

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        Dice f = new Dice();
        f.inputDice(sc);

        Dice s = new Dice();
        s.inputDice(sc);

        System.out.println((Dice.isSameDice(f, s))? "Yes" : "No");
    }

    static class Dice {
        private List<Integer> D = new ArrayList<>();

        void inputDice(Scanner sc)  {
            for (int i = 0; i < 6; i++) D.add(Integer.parseInt(sc.next()));
        }

        int getTop() {
            return D.get(0);
        }


        int getFront() {
            return D.get(1);
        }

        int getRight() {
            return D.get(2);
        }

        int getBack() {
            return D.get(3);
        }

        int getLeft() {
            return D.get(4);
        }

        int getBottom() {
            return D.get(5);
        }

        int getSum() {
            final int[] sum = {0};
            D.forEach(integer -> sum[0] += integer);
            return sum[0];
        }

        void searchFromTopAndFront(int top, int front) {
            while (!(getTop() == top && getFront() == front)) {
                if (getTop() == top && getFront() != front) {
                    slideRight();
                } else if (getTop() != top && getFront() == front) {
                    moveE();
                } else {
                    moveN();
                }
            }
        }

        void move(String t) {
            for (String s : t.split("")) {
                switch (s) {
                    case "N":
                        moveN();
                        break;
                    case "E":
                        moveE();
                        break;
                    case "W":
                        moveW();
                        break;
                    case "S":
                        moveS();
                        break;
                }
            }
        }

        private void slideRight() {
            List<Integer> tmp = new ArrayList<>();

            tmp.add(D.get(0));
            tmp.add(D.get(3));
            tmp.add(D.get(1));
            tmp.add(D.get(4));
            tmp.add(D.get(2));
            tmp.add(D.get(5));

            D = new ArrayList<>(tmp);
        }

        private void slideLeft() {
            slideRight();
            slideRight();
            slideRight();
        }

        private void moveN() {
            List<Integer> tmp = new ArrayList<>();

            tmp.add(D.get(1));
            tmp.add(D.get(5));
            tmp.add(D.get(2));
            tmp.add(D.get(3));
            tmp.add(D.get(0));
            tmp.add(D.get(4));

            D = new ArrayList<>(tmp);
        }

        private void moveE() {
            List<Integer> tmp = new ArrayList<>();

            tmp.add(D.get(3));
            tmp.add(D.get(1));
            tmp.add(D.get(0));
            tmp.add(D.get(5));
            tmp.add(D.get(4));
            tmp.add(D.get(2));

            D = new ArrayList<>(tmp);
        }

        private void moveW() {
            moveE();
            moveE();
            moveE();
        }

        private void moveS() {
            moveN();
            moveN();
            moveN();
        }

        private void print() {
            D.forEach(integer -> System.out.printf("%d ", integer));

            System.out.println();
        }

        private boolean compareTo(Dice d) {
            return getFront() == d.getFront() &&
                    getRight() == d.getRight() &&
                    getBack() == d.getBack() &&
                    getLeft() == d.getLeft() &&
                    getTop() == d.getTop() &&
                    getBottom() == d.getBottom();

        }

        private boolean isDuplicate() {
            Map<Integer, Integer> map = new HashMap<>();

            D.forEach(integer -> {
                if (!map.containsKey(integer)) map.put(integer, 0);
                map.put(integer, map.get(integer) + 1);
            });

            final boolean[] res = {false};
            map.forEach((integer, integer2) -> {
                if (integer2 > 1) res[0] = true;
            });

            return res[0];
        }

        static boolean isSameDice(Dice f, Dice s) {
            if (f.getSum() != s.getSum()) return false;

            if (f.getTop() == f.getFront()) {
                while (f.getTop() == f.getFront()) {
                    f.slideRight();
                    f.moveN();
                }
            } else {
                for (int i = 0; i < 6; i++) {
                    if (f.getTop() == s.getTop() && f.getFront() == s.getFront()) break;

                    if (s.getTop() == f.getTop() && s.getFront() != f.getFront()) {
                        s.slideRight();
                    } else if (s.getTop() != f.getTop() && s.getFront() == f.getFront()) {
                        s.moveE();
                    } else {
                        s.moveN();
                    }
                }
            }

            f.print();
            s.print();

            return f.compareTo(s);
        }
    }
}