##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
| # | Title | Points | Solved | Admin | |
|---|---|---|---|---|---|
There are books on a shelf, numbered from to . From left to right, their current order is the permutation .
The shelf is sorted if the books appear in the order .
In one operation, you may choose the leftmost or the rightmost book, remove it from the shelf, and insert it at any position. In particular, you may insert it at either end of the shelf.
Find the minimum number of operations needed to sort the shelf.
The input consists of multiple test cases.
For each test case, output the minimum number of operations needed to sort the shelf.
One optimal sequence is:
One operation cannot be enough. If book is moved, the remaining books are in the order ; if book is moved, they are in the order . Neither order can be part of the sorted shelf.
The books are initially in decreasing order. Any two books that are never moved would stay in decreasing relative order, so at least four books must be moved.
Four operations are sufficient: