#acl1f. [acl1_f]Center Rearranging
[acl1_f]Center Rearranging
Problem Statement
Given are integer sequences and of length . Each of these two sequences contains three copies of each of . In other words, and are both arrangements of .
Tak can perform the following operation to the sequence arbitrarily many times:
- Pick a value from and call it . contains exactly three copies of . Remove the middle element of these three. After that, append to the beginning or the end of .
Check if he can turn into . If he can, print the minimum required number of operations to achieve that.
Constraints
- and are both arrangements of .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
... ...
Output
If Tak can turn into , print the minimum required number of operations to achieve that. Otherwise, print .
Sample Input 1
3
2 3 1 1 3 2 2 1 3
1 2 2 3 1 2 3 1 3
Sample Output 1
4
For example, Tak can perform operations as follows:
2 3 1 1 3 2 2 1 3
(The initial state)2 2 3 1 1 3 2 1 3
(Pick and append it to the beginning)2 2 3 1 3 2 1 3 1
(Pick and append it to the end)1 2 2 3 1 3 2 3 1
(Pick and append it to the beginning)1 2 2 3 1 2 3 1 3
(Pick and append it to the end)
Sample Input 2
3
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
Sample Output 2
0
Sample Input 3
3
2 3 3 1 1 1 2 2 3
3 2 2 1 1 1 3 3 2
Sample Output 3
-1
Sample Input 4
8
3 6 7 5 4 8 4 1 1 3 8 7 3 8 2 4 7 5 2 2 6 5 6 1
7 5 8 1 3 6 7 5 4 8 1 3 3 8 2 4 2 6 5 6 1 4 7 2
Sample Output 4
7