#t258. 车厢重组

车厢重组

Carriage Reorganization

Problem Description

Next to an old-style train station there is a bridge whose deck can rotate horizontally around a pier in the middle of the river. A station worker found that the bridge can hold at most two carriages. By rotating the bridge 180180 degrees, the positions of two adjacent carriages can be swapped, and this method can be used to reorder the carriages. He was therefore put in charge of using this bridge to arrange the incoming carriages in ascending order of carriage number. After he retired, the train station decided to automate this work; an important part of it is to write a program that, given the initial order of the carriages, computes the minimum number of steps needed to sort the carriages.

Input Format

Two lines in total.

The first line is the total number of carriages NN (≤10000\le 10000).

The second line contains NN distinct numbers representing the initial order of the carriages.

(Note: in the actual data the numbers may not all be on the same line; they might be split across multiple lines.)

Output Format

One integer: the minimum number of rotations (swaps).

Sample Input

4
4 3 2 1

Sample Output

6