Alice has found an array containing non-negative integers, where the -th element is named . But she only likes an array if all elements of it are equal.
In order to make Alice happy, Bob has come up with an idea. He has a small array with non-negative integers, where the -th element is named . Bob can do the following operation any number of times (possibly zero):
Select any integer where .
Select any subset containing distinct indices of . Let .
Select any integer .
For each element , assign .
However, because doing this operation requires so much energy, Bob asked you to determine the minimum number of operations required to make all elements of equal or report if it is impossible. Can you make both Alice and Bob happy?
The input contains three lines.
The first line contains two integers and .
The second line contains integers representing where the -th integer represents .
The third line contains integers representing where the -th integer represents .
Print an integer in a single line denoting the minimum number of operations required to make all elements of equal if it is possible, otherwise print .
Input | Output |
---|---|
3 2 1 3 5 6 2 | 2 |
Input | Output |
---|---|
2 1 1 4 2 | -1 |