Sorting a permutation

Given a permutation of {1,,n}\{1, \dots, n\}, you must sort it in increasing order. The only operation allowed is to reverse the first ii elements of the current permutation, for any 2in2 \le i \le n.

For instance, in one step we can transform [3, 5, 2, 4, 1] into [5, 3, 2, 4, 1], [2, 5, 3, 4, 1], [4, 2, 5, 3, 1] and [1, 4, 2, 5, 3].

Given a permutation, what is the minimum number of steps to sort it?

Input

Input consists of several permutations, each with an nn between 1 and 18, followed by nn different numbers between 1 and nn.

Output

For every permutation, print the minimum number of operations to sort it.

Problem information

Author: Josep Grané

Generation: 2026-01-25T11:58:23.267Z

© Jutge.org, 2006–2026.
https://jutge.org