eolymp
bolt
Try our new interface for solving problems
Problems

Shelves

Shelves

On the shelf that runs around the perimeter of the library reading room, there are n volumes of works of classic indexed from 1 to n. Volumes are in disarray. The librarian decided to order the volumes, i.e. to place them so that for all i from 1 to n1 the volume i will stand side by side with the volume i + 1. There is a lot of volumes, so the librarian would like to minimize the number of his actions. The action is to swap in places any two volumes. It is required to find the minimum number of actions needed to arrange in order all set of volumes.

Input

The first line contains the number n (1n3000), each of the next n lines contains the volume number in the appropriate place. Each volume number occurs only once.

Output

Print one number - the minimum number of librarian actions.

Time limit 1 second
Memory limit 122.17 MiB
Input example #1
5
2
5
4
3
1
Output example #1
1

Example description: It is necessary to swap volumes 1 and 2.

Author Vladimir Pinaev, Fedor Menschiko