Free Figurines

Given two valid nesting configurations of n matryoshka dolls, find the minimum number of place and take-out moves to transform one into the other.

Medium6TreeGreedyDFSNo attempts yetTime limit1sMemory limit512 MB

Problem

A matryoshka set is nn wooden figurines numbered 1 to nn in increasing order of size. A figurine can be placed directly inside a larger figurine, and at most one figurine sits directly inside another figurine, whatever the sizes involved.

If figurine aa is directly inside figurine bb, then bb is the parent of aa. A figurine with no parent is free. Writing down the parent of every figurine describes the configuration of the whole set.

Two moves are allowed:

  • Place a free figurine inside a larger figurine that is free and currently empty.
  • Open a free figurine that is not empty and take out the figurine directly inside it.

You are given an initial configuration and a target configuration. Find the smallest number of moves that turns the initial configuration into the target configuration.

Input

The first line contains an integer nn (1n1000001 \le n \le 100000), the number of figurines.

The second line contains nn integers p1,p2,,pnp_1, p_2, \dots, p_n (0pkn0 \le p_k \le n) describing the initial configuration. pkp_k is 0 if figurine kk is free, and the parent of figurine kk otherwise.

The third line contains nn integers q1,q2,,qnq_1, q_2, \dots, q_n (0qkn0 \le q_k \le n) describing the target configuration in the same format.

Both configurations are valid: every figurine is smaller than its parent, and no two figurines have the same parent.

Output

Print one integer, the smallest number of moves.