import std.stdio;
import std.ascii;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

long[] calc(int x) {
    long p = 1;
    long[] Ret;
    for (int i = 0; i < x; i++) {
        Ret ~= p;
        p *= 10;
    }
    return Ret;
}
static P10 = calc(11);

long reverse(long x, long len, size_t from, size_t to) {
    //long len = countUntil!((a) => x < a)(P10);
    //writeln([x, len, from, to]);
    long y = (x % P10[len - from] / P10[len - to]);
    long z = 0;
    foreach (i; 0 .. (to - from)) {
        z += (y % P10[i + 1] / P10[i]) * P10[to - from - i - 1];
    }
    static if (false) {
        writeln("a: ", x / P10[len - from] * P10[len - from]);
        writeln("b: ", z * P10[len - to]);
        writeln("c: ", x / P10[len - to]);
    }
    return x / P10[len - from] * P10[len - from] + z * P10[len - to] + x % P10[len - to];
}

void main() {
    int N; scanf("%d\n", &N);
    string A = cast(string)(readln.chomp.split(" ").map!((x) => cast(char)(x.to!int - 1 + '0')).array);

    bool[long] step(in bool[long] x, ref bool[long] used) {
        bool[long] Ret;
        foreach (k, v; x) {
            foreach (i; 0 .. N) {
                foreach (j; i + 2 .. N + 1) {
                    long s = reverse(k, N, i, j);
                    if (s in used) continue;
                    used[s] = true;
                    Ret[s] = true;
                }
            }
        }
        return Ret;
    }

    long x1 = A.to!long;
    long y1 = (iota(0, N, 1).map!((c) => cast(char)(c + '0')).array).to!long;

    //writeln([x1, y1]);

    bool[long] a = [x1: true],
                 b = [y1: true];

    bool check() {
        foreach (k, v; a) {
            if (k in b) return true;
        }
        return false;
    }

    bool[long] a_used = a.dup, b_used = b.dup;
    for (int i = 0; ; i++) {
        if (check) {
            writeln(i * 2);
            break;
        }
        a = step(a, a_used);
        if (check) {
            writeln(i * 2 + 1);
            break;
        }
        b = step(b, b_used);
    }

    //writeln([a.length, b.length]);

}