CKL is dining with his friends. They sit around a table with a Lazy Susan on it. We number the people around the table from 1 to N consecutively in clock-wise order. Initially, there is exactly one dish in front of each person, and we also number them from 1 to N, which corresponds to the people’s number. However, not all of the diners wish to taste the dish in front of them, so we need to turn the tray and pass the dish to their front. A step to turn the tray is defined by passing the dishes originally in front of everyone to their left or right neighbors. A person is said to be served if the dish he or she wants to taste has been passed to his or her front at least once, or the dish is initially in the front of him or her. Find the minimum number of steps to turn the tray such that all of the diners are served.
Each test case begins with a line consists of an integer N (1 ≤ N ≤ 1000), denoting the number of diners and dishes. The second line contains N integers di (1 ≤ di ≤ N), representing that the i-th diner wish to taste the dish which is initially placed in front of the di-th diner.
The input is terminated by N = 0.
For each test case, output a line that consists of an integer, representing the minimum number of steps.