Problem: A permutation of N numbers from 1 to N A1, A2, ……, AN is given. You can perform the operation reverse (i, j) to reverse the order of the numbers in the interval [i, j] (1 ≤ i ≤ j ≤ N) for this permutation. For example, if reverse (2, 4) is applied to [1, 2, 3, 4, 5], it becomes [1, 4, 3, 2, 5]. Calculate how many operations you need to do at a minimum to get the permutation sorted in ascending order.

Constraints

* N is an integer

* 2 ≤ N ≤ 10

Input

The input is given in the following format.

> N
> A1 A2 …… AN
>

Output

Print the solution to the problem on one line.

Examples

Input

5
1 4 3 5 2


Output

2


Input

5
3 1 5 2 4


Output

4


Input

3
1 2 3


Output

0


Input

10
3 1 5 2 7 4 9 6 10 8


Output

9

Solution (in PYTHON3):

Solution:
from heapq import heappush, heappop
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve():
    N = int(readline())
    INF = 10**9
    A = tuple(map(int, readline().split()))
    B = tuple(range(1, N+1))
    def check(N, A, N1):
        que = [(0, A)]
        dist = {A: 0}
        S = [A]
        B = list(A)
        for cost in range(N1):
            T = []
            for v in S:
                if dist[v] < cost:
                    continue
                for i in range(N):
                    B[:] = v
                    for j in range(i+1, N):
                        B[i:j+1] = reversed(v[i:j+1])
                        key = tuple(B)
                        if cost+1 < dist.get(key, N1+1):
                            dist[key] = cost+1
                            T.append(key)
            S = T
        return dist
    D0 = check(N, A, (N-1)//2)
    D1 = check(N, B, (N-1)//2)
    ans = N-1
    for state, v in D0.items():
        if state in D1:
            ans = min(ans, v + D1[state])
    write("%d\n" % ans)
solve()