LearnPy Hub
← Arena
IntermediateRating 1300

Make One Row the Same

Each domino has a top and a bottom number (1–6). Rotating a domino swaps its two numbers. Print the fewest rotations so that every top is equal or every bottom is equal, or -1 if impossible.

Input

n; the tops; the bottoms.

Output

One integer.

Sample 1

Input
6
2 1 2 4 2 2
5 2 6 2 3 2
Output
2

Sample 2

Input
5
3 5 1 2 3
3 6 3 3 4
Output
-1
solution.py
Output appears here
Make One Row the Same · Python Arena problem · LearnPy Hub