#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.

{a}\{a\} is a sequence of length nn with kk different colors. The color of the ii-th element is aia_i (it is guaranteed that colors 1k1\sim k all appear in aa).

{b}\{b\} is a sequence of length mm. The color of the ii-th element is bib_i (it is guaranteed that each bib_i is one of the kk colors, but it is not guaranteed that all kk colors appear in bb). We may change the colors at some positions in bb and obtain a sequence bb' that still has length mm.

For bb' and aa, connect every pair of positions with the same color by a line segment of that color.

For example, when n=3,m=4,k=3,a={1,2,3},b={1,3,2,2}n=3,m=4,k=3,a=\{1,2,3\},b'=\{1,3,2,2\}, the connections look like this:

You are required that line segments of different colors do not intersect each other, and all kk colors must appear in bb. What is the minimum number of modifications?

Formally, let the modified valid sequence be bb'. You need to minimize:

i=1m[bibi]\sum_{i=1}^{m}[b_i\ne b'_i]

In the above case a={1,2,3},b={1,3,2,2}a=\{1,2,3\},b'=\{1,3,2,2\}, the connections have an intersection (between the red 22 and the purple 33).

But if we modify bb to {1,2,3,3}\{1,2,3,3\}, then the connections do not intersect and the conditions are satisfied:

Note:

  • For b={1,1,4,5}b' = \{1,1,4,5\}, the connections also do not intersect, but bb' contains colors outside the kk colors (there are 44 and 55), so this bb' is invalid.
  • For b={1,1,1,1}b' = \{1,1,1,1\}, the connections also do not intersect, but bb' does not contain all colors in 1k1\sim k (it lacks 22 and 33), so this bb' 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 n,mn,m, representing the number of elements in sequences a,ba,b respectively.

The second line contains nn integers, where the ii-th integer denotes aia_i.

The third line contains mm integers, where the ii-th integer denotes bib_i.

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 {1,3,2,2}\{1,3,2,2\} into {1,2,2,3}\{1,2,2,3\}.

It can be proven that this uses the minimum number of modifications.

[Sample #2 Explanation]

Modify {1,2,3,3,3}\{1,2,3,3,3\} into {1,2,3,3,4}\{1,2,3,3,4\}.

It can be proven that this uses the minimum number of modifications.


This problem enables bundled test and subtask dependencies.

Time limit: 2 s.

Subtask\text{Subtask} n,mn,m\le Score Dependencies
11 55 None
22 50005000 3535 11
33 10510^5 3030 1,21,2
44 2×1062\times 10^6 1,2,31,2,3
  • For 100%100\% of the testdata, it is guaranteed that 1n,m2×1061\le n,m\le 2\times 10^6, 1ai,bin1\le a_i,b_i \le n. Let maxi=1nai=k\max\limits_{i=1}^n{a_i} = k. It is guaranteed that 1k1\sim k all appear in aa, and 1kn1\le k \le n.

Translated by ChatGPT 5