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 functools import lru_cache
import sys
from collections import deque
sys.setrecursionlimit(1000000)
	
sorted_a = ""
m = dict({})

N = int(input())
A = ''.join(list(map(lambda x: str(int(x)-1), input().split())))
sorted_a = ''.join(list(sorted(A)))

q = deque()
q.append((A, 0))
m[A] = 0
while True:
	a, x = q.popleft()
	if a in m and m[a] < x:
		continue
	if x == (len(a)-1) // 2:
		break;

	for i in range(len(a)):
		for j in range(i+2, len(a)+1):
			next_a = a[:i:] + a[::-1][len(a)-j:len(a)-i:] + a[j::]
			if next_a in m:
				continue

			q.append((next_a, x+1))
			m[next_a] = x+1
			
m2 = dict({})
q = deque()
q.append((sorted_a, 0))
m2[sorted_a] = 0
while True:
	a, x = q.popleft()
	if a in m2 and m2[a] < x:
		continue
	if x == (len(a)-1) // 2:
		break;

	for i in range(len(a)):
		for j in range(i+2, len(a)+1):
			next_a = a[:i:] + a[::-1][len(a)-j:len(a)-i:] + a[j::]
			if next_a in m2:
				continue
			q.append((next_a, x+1))
			m2[next_a] = x+1

ret = sys.maxsize
for a, x in m.items():
	if a in m2:
		ret = min(ret, x + m2[a])
	
if ret == sys.maxsize:
	print(len(A) - 1)
else:
	print(ret)