atcoder#AGC038F. [AGC038F] Two Permutations

[AGC038F] Two Permutations

Score : 16001600 points

Problem Statement

Snuke has permutations (P0,P1,,PN1)(P_0,P_1,\cdots,P_{N-1}) and (Q0,Q1,,QN1)(Q_0,Q_1,\cdots,Q_{N-1}) of (0,1,,N1)(0,1,\cdots,N-1).

Now, he will make new permutations AA and BB of (0,1,,N1)(0,1,\cdots,N-1), under the following conditions:

  • For each ii (0iN10 \leq i \leq N-1), AiA_i should be ii or PiP_i.
  • For each ii (0iN10 \leq i \leq N-1), BiB_i should be ii or QiQ_i.

Let us define the distance of permutations AA and BB as the number of indices ii such that AiBiA_i \neq B_i. Find the maximum possible distance of AA and BB.

Constraints

  • 1N1000001 \leq N \leq 100000
  • 0PiN10 \leq P_i \leq N-1
  • P0,P1,,PN1P_0,P_1,\cdots,P_{N-1} are all different.
  • 0QiN10 \leq Q_i \leq N-1
  • Q0,Q1,,QN1Q_0,Q_1,\cdots,Q_{N-1} are all different.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

P0P_0 P1P_1 \cdots PN1P_{N-1}

Q0Q_0 Q1Q_1 \cdots QN1Q_{N-1}

Output

Print the maximum possible distance of AA and BB.

4
2 1 3 0
0 2 3 1
3

For example, if we make A=(0,1,2,3)A=(0,1,2,3) and B=(0,2,3,1)B=(0,2,3,1), the distance will be 33, which is the maximum result possible.

10
0 4 5 3 7 8 2 1 9 6
3 8 5 6 4 0 2 1 7 9
8
32
22 31 30 29 7 17 16 3 14 9 19 11 2 5 10 1 25 18 15 24 20 0 12 21 27 4 26 28 8 6 23 13
22 3 2 7 17 9 16 4 14 8 19 26 28 5 10 1 25 18 15 13 11 0 12 23 21 20 29 24 27 6 30 31
28