Problem Statement
There are $N$ boxes. For $i = 1, 2, \ldots, N$, the $i$-th box contains $A_i$ red balls and $B_i$ blue balls.
You are also given two permutations $P = (P_1, P_2, \ldots, P_N)$ and $Q = (Q_1, Q_2, \ldots, Q_N)$ of $(1, 2, \ldots, N)$.
Takahashi can repeat the following operation any number of times, possibly zero:
- Choose an integer
$1 \leq i \leq N$, and take all the balls from the$i$-th box into his hand. - Put all the red balls in his hand into the
$P_i$-th box. - Put all the blue balls in his hand into the
$Q_i$-th box.
His goal is to make a state where all boxes other than the $X$-th box contain no balls by repeating the above operations. Determine whether it is possible to achieve his goal, and if possible, print the minimum number of operations needed to achieve it.
Constraints
$2 \leq N \leq 2 \times 10^5$$0 \leq A_i, B_i \leq 1$$1 \leq P_i, Q_i \leq N$$P$and$Q$are permutations of$(1, 2, \ldots, N)$.$1 \leq X \leq N$- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
P_1 P_2 \ldots P_N
Q_1 Q_2 \ldots Q_N
Output
If it is impossible for Takahashi to achieve a state where all boxes other than the $X$-th box contain no balls, print -1. If it is possible, print the minimum number of operations needed to achieve it.
样例
5 3
0 1 0 1 0
0 0 1 0 1
4 1 2 3 5
3 4 5 2 1
4
5 3
0 0 0 0 0
0 0 0 0 0
4 1 2 3 5
3 4 5 2 1
0
2 2
1 1
1 1
1 2
1 2
-1
10 10
0 0 0 0 0 0 1 0 1 0
0 0 0 0 1 1 0 0 1 0
1 4 9 5 8 2 3 6 10 7
7 4 9 10 6 3 1 2 8 5
8
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。