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;

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[string] step(in bool[string] x) {
        bool[string] Ret;
        foreach (k, v; x) {
            foreach (i; 0 .. N) {
                foreach (j; i + 2 .. N + 1) {
                    char[] r = cast(char[])(k[i .. j].dup);
                    reverse(r);
                    string s = k[0 .. i] ~ cast(string)r ~ k[j .. $];
                    if (s in x) continue;
                    Ret[s] = true;
                }
            }
        }
        return Ret;
    }

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

    //writeln([x1, y1]);

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

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

    for (int i = 0; ; i++) {
        if (check) {
            writeln(i * 2);
            break;
        }
        a = step(a);
        if (check) {
            writeln(i * 2 + 1);
            break;
        }
        b = step(b);
    }

}