P18155 [NWERC 2026] DB Adventures
Description
Dorothea and the other NWERC contestants from her university are taking the train to Karlsruhe, but the journey has turned out to be rather exhausting. They sit in a big circle on the floor (due to an overcrowded train), when Dorothea suggests playing a few rounds of their favourite card game. They all have a great time until it is time to change trains. The next train is just as full as the last one, and once again they find themselves sitting in a big circle on the floor.
They want to continue their card game, but their order of play might have changed now. Before they can play, they need to return to their original order, up to some cyclic shift. To do this, some people may stand up and arbitrarily swap places among each other before they sit down again. See the figure in the Sample 3 Explanation for an example.
It has been a long day, and nobody really feels like getting up. What is the minimum number of people that need to stand up?
Input Format
The input consists of:
- One line with an integer $n$ ($3 \leq n \leq 3 \cdot 10^5$), the number of people sitting in a circle.
- One line with $n$ integers $p_1, p_2, \dots, p_n$ ($1 \leq p_i \leq n$ for each $i$), the original seating in clockwise order. Person $p_i$ is the $i$th person in the order.
- One line with $n$ integers $q_1, q_2, \dots, q_n$ ($1 \leq q_i \leq n$ for each $i$), the current seating in clockwise order. Person $q_i$ is the $i$th person in the order.
The people are numbered from $1$ to $n$. Both $p_1, \ldots, p_n$ and $q_1, \ldots, q_n$ are permutations of $\{1,2,\ldots,n\}$. The orders are cyclic, that is, the first person sits after the last person in each order. Note that the given cyclic orders may be the same.
Output Format
Output the minimum number of people that need to stand up to get back to the original cyclic order.