#K8066. Minimum Moves to Rearrange Blocks

    ID: 35580 Type: Default 1000ms 256MiB

Minimum Moves to Rearrange Blocks

Minimum Moves to Rearrange Blocks

You are given a sequence of blocks arranged in an initial order and a target order. Your task is to determine the minimum number of moves required to transform the initial arrangement into the target arrangement. In one move, you may select any contiguous subarray of blocks and reverse its order.

Formally, let \( A = [a_1, a_2, \dots, a_n] \) be the initial arrangement and \( B = [b_1, b_2, \dots, b_n] \) be the target arrangement. In one operation, you can choose indices \( i \) and \( j \) such that \( 1 \le i < j \le n \) and reverse the subarray \( A[i \dots j] \). It can be proven that at most two moves are sufficient to achieve the target arrangement.

Determine the minimum number of moves needed.

inputFormat

The input is read from standard input (stdin) as follows:

The first line contains an integer ( n ) ((1 \le n \le 10^5)), the number of blocks.

The second line contains ( n ) space-separated integers representing the initial arrangement.

The third line contains ( n ) space-separated integers representing the target arrangement.

outputFormat

Print a single integer on the standard output (stdout) — the minimum number of moves required to transform the initial arrangement into the target arrangement.## sample

5
1 2 3 4 5
1 2 3 4 5
0

</p>