Minimum overtakes

Given a starting order and a finishing order of up to 24 cars, print the minimum number of adjacent swaps that turn the start into the finish.

Medium4SortingArrayCombinatoricsNo attempts yetTime limit2sMemory limit512 MB

Problem

The world final of formula 17 is held in Nlogonia. The cars line up on the starting grid in a fixed order and start the race, and the order in which they cross the finish line is the final standing.

An overtake is one car passing the single car directly ahead of it. One overtake swaps the order of those two cars and leaves every other car where it was.

Given the starting order and the finishing order, find the smallest number of overtakes that can explain the result.

Input

The input holds several test cases, and each test case uses three lines. The first line has the number of cars NN. Each car is identified by a number from 11 to NN. The second line has the NN car numbers in starting grid order. The third line has the same numbers in finishing order. The input continues until the end of the file.

Constraints

  • 2N242 \le N \le 24

Output

For each test case print one line with the minimum number of overtakes needed to get from the starting order to the finishing order.