There is a queue $Q$ and two stacks $A$ and $B$. The following seven operations are available.
Since $Q$ is a queue (FIFO), a number is always removed from the front and inserted at the back. Since $A$ and $B$ are stacks (LIFO), a number is always removed from the top and pushed onto the top.
Each operation counts as one, regardless of the value of $n$.
Initially the queue holds all of the numbers and both stacks are empty. You want the numbers in the queue, read from front to back, to be in ascending order. Find the minimum number of operations required. (When sorting is finished, all numbers must be back in the queue and both stacks must be empty.)
For example, if the queue is (4 3 1 2 0), it can be sorted in 3 operations: QA(2), QQ(2), AQ(2). If the queue is (5 4 1 3 2 0), it can be sorted in 4 operations: QB(2), QQ(1), QB(2), BQ(4).
The input consists of several test cases. Each test case is given on one line whose first integer is $N$, the count of numbers in the queue. The next $N$ integers are the contents of the queue, given in order from the front.
Every number in the queue is an integer between $0$ and $N-1$ inclusive, and each number appears exactly once. $N$ does not exceed $10$.
The last line of the input contains a single $0$, which is not processed and marks the end of the input.
For each test case, print on its own line the minimum number of operations needed to sort the queue in ascending order.