#P10395. 青蛙寻青。
青蛙寻青。
Background
After failing several times, the little frog’s way of thinking began to change.
It began to search for what makes it a frog.
It began to look for help from other frogs.
It is undergoing a transformation.
It is being refined.
It will become light!
It gave itself a new name — Frog Cyan (qwq), because the name is very cute.
Problem Description
White light can be split into cyan light and many other colors of light.
is a sequence of length with different colors. The color of the -th element is (it is guaranteed that colors all appear in ).
is a sequence of length . The color of the -th element is (it is guaranteed that each is one of the colors, but it is not guaranteed that all colors appear in ). We may change the colors at some positions in and obtain a sequence that still has length .
For and , connect every pair of positions with the same color by a line segment of that color.
For example, when , the connections look like this:

You are required that line segments of different colors do not intersect each other, and all colors must appear in . What is the minimum number of modifications?
Formally, let the modified valid sequence be . You need to minimize:
In the above case , the connections have an intersection (between the red and the purple ).
But if we modify to , then the connections do not intersect and the conditions are satisfied:

Note:
- For , the connections also do not intersect, but contains colors outside the colors (there are and ), so this is invalid.
- For , the connections also do not intersect, but does not contain all colors in (it lacks and ), so this is also invalid.
In particular, if no matter how you modify it you still cannot satisfy the requirements, output -1.
Input Format
The first line contains two integers , representing the number of elements in sequences respectively.
The second line contains integers, where the -th integer denotes .
The third line contains integers, where the -th integer denotes .
Output Format
One line with one integer, representing the minimum number of modifications required to satisfy the conditions.
If no matter how you modify it you still cannot satisfy the requirements, output -1.
3 4
1 2 3
1 3 2 2
2
5 5
1 2 3 4 4
1 2 3 3 3
1
5 10
1 2 3 4 5
1 2 2 3 2 2 2 4 5 4
3
10 2
1 2 1 2 2 2 2 2 2 2
2 2
-1
Hint
[Sample #1 Explanation]
Modify into .
It can be proven that this uses the minimum number of modifications.
[Sample #2 Explanation]
Modify into .
It can be proven that this uses the minimum number of modifications.
This problem enables bundled test and subtask dependencies.
Time limit: 2 s.
| Score | Dependencies | ||
|---|---|---|---|
| None | |||
- For of the testdata, it is guaranteed that , . Let . It is guaranteed that all appear in , and .
Translated by ChatGPT 5