Train
InterviewTime limit1sMemory limit512 MB
Count the fewest round trips a train needs to drop its wagons from the back at their assigned cities in order.
Problem
Every year a large shipment of raw materials arrives at an island. The materials are brought to the port, and from there they are delivered by train to every city on the island. All cities lie along the coast and are numbered consecutively from to . The train's route runs all the way around the island, passing through every city.
The train is a single locomotive pulling a number of wagons. Each wagon carries a different kind of raw material, and each city receives a different material. There are exactly as many wagons as there are cities. Normally the wagons are arranged so that at each city the train drops off its last wagon and moves on to the next city, finishing every delivery in a single loop. This time, however, the materials were labeled incorrectly and the order of the wagons got mixed up.
The engineer wants to know the minimum number of loops needed to deliver every material to its correct city, given that he can only ever detach the last wagon and can never reattach a wagon once it has been dropped off. The start and end station is city , where the port is located. A new loop begins the moment the train sets off toward city .
Input
The first line contains one integer (), the number of cities on the island. The second line contains integers (, and whenever ), where is the kind of raw material loaded in the -th wagon counting from the locomotive. The third line contains pairwise distinct integers , where is the material ordered by city , for . The numbers on the second and third lines are separated by single spaces. You may assume the two sets are equal: .
Output
Print one integer: the minimum number of loops needed to deliver every material to its correct city.
Hint

Explanation: The figure above shows a case with cities. In the first loop only the wagon carrying material is detached, in the second loop the wagon carrying material , and in the third and final loop the wagons carrying materials , , and are detached in that order, for a total of loops.