Solution
C++ []\nclass Solution {\npublic:\n    int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {\n        int flips = 0;\n\n        const int num = tops
```C++ []\nclass Solution {\npublic:\n    int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {\n        int flips = 0;\n\n        const int num = tops.size();\n\n        int top = tops[0];\n        int bottom = bottoms[0];\n\n        int symmetricals = (top == bottom) ? 1 : 0;\n\n        for (int i = 1; i < num; i++) {\n            int currTop = tops[i];\n            int currBottom = bottoms[i];\n\n            if (currTop == currBottom) {\n                symmetricals++;\n            }\n            if (currTop == top || currBottom == bottom) {\n                if (currTop != top) {\n                    top = -1;\n                }\n                else if (currBottom != bottom) {\n                    bottom = -1;\n                }\n            }\n            else if (currTop == bottom && currBottom == top) {\n                flips++;\n            } else if (currBottom == top) {\n                flips++;\n                bottom = -1;\n            } else if (currTop == bottom) {\n                flips++;\n                top = -1;\n            }\n            else {\n                return -1;\n            }\n        }\n        if (top == -1 && bottom == -1) {\n            return -1;\n        }\n        if (flips * 2 > num - symmetricals) {\n            flips = (num - flips) - symmetricals;\n        }\n        return flips;\n    }\n};\n```\n\n```Python3 []\nclass Solution:\n    def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:\n        n = len(tops)\n        ans = float("inf")\n        if len(tops)!=len(bottoms):\n            return -1\n        def get_ans(num):\n            ans = float("inf")\n        \n            ntop = 0\n            nbottom = 0\n            for t,b in zip(tops, bottoms):\n                if t!=num and b!=num:\n                    return -1\n                elif t!=num:\n                    ntop +=1\n                elif b!=num:\n                    nbottom +=1\n            return min(ntop, nbottom)\n            \n        ts = get_ans(tops[0]) \n        if (ts == -1):\n            return get_ans(bottoms[0])\n        else:\n            return ts\n```\n\n```Java []\nclass Solution {\n    public int minDominoRotations(int[] A, int[] B) {\n        int ans = -1;        \n        for (int val = 1; val <= 6; val++) {\n            int currAns = helper(A, B, val);\n            \n            if (currAns != -1 && (ans == -1 || ans > currAns)) {\n                ans = currAns;\n            }\n        }\n        return ans;\n    }\n    private int helper(int[] A, int[] B, int val) {\n        int ansTop = 0, ansBottom = 0;\n        \n        for (int i = 0; i < A.length; i++) {\n            if (A[i] != val && B[i] != val) {\n                return -1;\n            } else if (A[i] != val) {\n                ansTop++;\n            } else if (B[i] != val) {\n                ansBottom++;\n            }\n        }\n        return Math.min(ansTop, ansBottom);\n    }\n}\n```
['C++', 'Java', 'Python3']