Problem:
Reverse Sort
1 ∼ NまでのN個の数字の順列A
1
, A
2
, ……, A
N
が与えられる。
この順列に対して区間[i, j](1 ≤ i ≤ j ≤ N)の数字の順序を逆順にする操作reverse(i, j)を行うことが出来る。
例えば、[1, 2, 3, 4, 5]に対してreverse(2, 4)を適用すると[1, 4, 3, 2, 5]となる。
最小で何回の操作を行えば順列を昇順にソートされた状態にすることが出来るか計算せよ。
Input
入力は以下の形式で与えられる。
N
A
1
A
2
…… A
N
Constraints
Nは整数である
2 ≤ N ≤ 10
Output
問題の解を1行に出力せよ。
Sample Input 1
5
1 4 3 5 2
Output for the Sample Input 1
2
reverse(4,5):1 4 3 2 5
reverse(2,4):1 2 3 4 5
のようにすればよい。
Sample Input 2
5
3 1 5 2 4
Output for the Sample Input 2
4
例えば
reverse(1,2):1 3 5 2 4
reverse(3,5):1 3 4 2 5
reverse(2,4):1 2 4 3 5
reverse(3,4):1 2 3 4 5
のようにすればよい。
Sample Input 3
3
1 2 3
Output for the Sample Input 3
0
Sample Input 4
10
3 1 5 2 7 4 9 6 10 8
Output for the Sample Input 4
9

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

void bfs(vector<int> v, map<vector<int>, int>& m) {
    const int N = v.size();
    queue<vector<int>> que;
    que.push(v);
    m[v] = 0;
    while(!que.empty()) {
        auto now = que.front();
        que.pop();
        int cost = m[now];
        if(cost >= (N-1)/2) {
            continue;
        }
        for(int i=0; i<N; ++i) {
            for(int j=i+2; j<=N; ++j) {
                auto next = now;
                reverse(next.begin()+i, next.begin()+j);
                if(m.count(next) == 0) {
                    m[next] = cost + 1;
                    que.push(next);
                }
            }
        }
    }
}

int main() {
    int N;
    cin >> N;
    vector<int> v(N);
    for(int i=0; i<N; ++i) {
        cin >> v[i];
    }
    auto v2 = v;
    sort(v2.begin(), v2.end());
    map<vector<int>, int> m1, m2;
    int res = N - 1;
    bfs(v, m1);
    bfs(v2, m2);
    do {
        if(m1.count(v2) == 0 || m2.count(v2) == 0) {
            continue;
        }
        res = min(res, m1[v2] + m2[v2]);
    } while(next_permutation(v2.begin(), v2.end()));
    cout << res << endl;
}
accuracy: 1
time_ms: 1320
memory_kb: 83384
final_rank: 11

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    iota(ALL(A),0);
    map<vector<int>,int> mp;
    queue<pair<vector<int>,int>> Q;
    Q.push({A,0});
    mp[A] = 0;
    while(!Q.empty()) {
        auto [V,D] = Q.front();
        Q.pop();
        rep(i,0,N) {
            rep(j,i+2,N+1) {
                reverse(V.begin()+i,V.begin()+j);
                if (!mp.count(V)) {
                    mp[V] = D+1;
                    if (D+1 != 4) Q.push({V,D+1});
                }
                reverse(V.begin()+i,V.begin()+j);
            }
        }
    }
    vector<int> B(N);
    rep(i,0,N) cin >> B[i], B[i]--;
    int ANS = 9;
    map<vector<int>,int> mp2;
    Q.push({B,0});
    mp2[B] = 0;
    while(!Q.empty()) {
        auto [V,D] = Q.front();
        Q.pop();
        if (mp.count(V)) chmin(ANS, mp[V]+D);
        if (D == 4) continue;
        rep(i,0,N) {
            rep(j,i+2,N+1) {
                reverse(V.begin()+i,V.begin()+j);
                if (!mp2.count(V)) {
                    mp2[V] = 0;
                    Q.push({V,D+1});
                }
                reverse(V.begin()+i,V.begin()+j);
            }
        }
    }
    cout << ANS << endl;
}
accuracy: 1
time_ms: 820
memory_kb: 85196
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define srep(i, s, t) for(int i = (s); i < (t); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
using i64 = long long;
using f64 = long double;
i64 floor_div(const i64 n, const i64 d) { assert(d != 0); return n / d - static_cast<i64>((n ^ d) <  0 && n % d != 0); }
i64  ceil_div(const i64 n, const i64 d) { assert(d != 0); return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0); }

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int N; cin >> N;

    auto encode = [&](vector<int>& vec) -> i64 {
        i64 res = 0;
        for(auto e : vec) res = res * N + e;
        return res;
    };
    auto decode = [&](i64 x) -> vector<int> {
        vector<int> res(N);
        int pos = N - 1;
        while(x > 0) {
            res[pos--] = x % N;
            x /= N;
        }
        return res;
    };
    
    vector<int> S(N), T(N);
    rep(i, N) {
        cin >> S[i]; S[i]--;
        T[i] = i;
    }

    auto F = [&](vector<int> X) {
        unordered_map<i64, int> dist;
        queue<pair<int, vector<int>>> q;
        q.push({0, X});
        dist[encode(X)] = 0;
        while(not q.empty()) {
            auto [dV, V] = q.front(); q.pop();
            if(not(dV <= N / 2 - 1)) continue;
            for(int L = 0; L < N; L++) {
                for(int R = L + 2; R <= N; R++) {
                    reverse(V.begin() + L, V.begin() + R);
                    if(not dist.count(encode(V))) {
                        dist[encode(V)] = dV + 1;
                        q.push({dV + 1, V});
                    }
                    reverse(V.begin() + L, V.begin() + R);
                }
            }
        }
        return dist;
    };
    
    auto dS = F(S);
    auto dT = F(T);
    int ans = N - 1;
    for(auto [x, dx] : dS) {
        if(dT.count(x)) {
            chmin(ans, dx + dT[x]);
        }
    }
    cout << ans << "\n";
}
accuracy: 1
time_ms: 4030
memory_kb: 201548
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")

int perm[12];

void init() {
  perm[0] = 1;
  for (int i = 1; i < 12; ++i) {
    perm[i] = perm[i-1] * i;
  }
  return;
}

int N; 

inline int conv(vector<int> &vec) {
  int ret = 0;
  for (int i = 0; i < N; ++i) {
    int c = 0;
    for (int j = i+1; j < N; ++j) c += (vec[i] > vec[j]);
    ret += c * perm[N - 1 - i];
  }
  return ret;
}

int main() {
  init();
  cin >> N;
  vector<int> A(N);
  for (auto &a : A) cin >> a, a--;
  
  int siz = perm[N];
  vector<vector<int>> graph(siz);
  
  vector<int> ID(N);
  iota(ID.begin(), ID.end(), 0);
  map< vector<int>, int > dist;
  {
    queue< vector<int> > que;
    dist[ID] = 0;
    que.push(ID);
    
    while (que.size()) {
      auto vec = que.front();
      que.pop();
      if (dist[vec] >= 4) continue;
      
      for (int a = 0; a < N; ++a) {
        for (int b = a+2; b <= N; ++b) {
          vector<int> nvec = vec;
          reverse(nvec.begin() + a, nvec.begin() + b);
          if (dist.find(nvec) == dist.end()) {
            dist[nvec] = dist[vec] + 1;
            que.push(nvec);
          }
        }
      }   
    }
    
  }
  // cout << dist.size() << endl;
  
  int res = 9;
  for (auto [vec, d] : dist) {
    
    vector<int> pos(N);
    for (int i = 0; i < N; ++i) {
      pos[vec[i]] = i;
    }
    vector<int> Q(N);
    for (int i = 0; i < N; ++i) {
      Q[i] = pos[A[i]];
    }
    
    // if (flg) {
    //   for (int v : Q) {cout << v << " ";}
    //   cout << ": " << d << endl;
    // }
    
    if (dist.find(Q) != dist.end()) {
      res = min(res, d + dist[Q]);
    }
  }
  cout << res << endl;
  
  
  return 0;
}
accuracy: 1
time_ms: 530
memory_kb: 136456
final_rank: 12

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        a[i] -= 1;
    }

    // 前計算
    map<vector<int>, int> mp;
    queue<pair<vector<int>, int>> q;
    {
        vector<int> b(n);
        iota(b.begin(), b.end(), 0);
        mp[b] = 0;
        q.push({b, 0});
        while (q.size()) {
            auto c = q.front().first;
            int cost = q.front().second;
            q.pop();
            for (int i = 0; i < n; ++i) {
                for (int j = i + 1; j < n; ++j) {
                    reverse(c.begin() + i, c.begin() + j + 1);
                    if (mp.find(c) == mp.end()) {
                        mp[c] = cost + 1;
                        if (cost + 1 <= 3) {
                            q.push({c, cost + 1});
                        }
                    }
                    reverse(c.begin() + i, c.begin() + j + 1);
                }
            }
        }
    }

    int res = n - 1;
    set<vector<int>> st;
    {
        st.insert(a);
        q.push({a, 0});
        while (q.size()) {
            auto c = q.front().first;
            int cost = q.front().second;
            q.pop();
            if (mp.find(c) != mp.end()) {
                res = min(res, cost + mp[c]);
            }
            for (int i = 0; i < n; ++i) {
                for (int j = i + 1; j < n; ++j) {
                    reverse(c.begin() + i, c.begin() + j + 1);
                    if (st.find(c) == st.end()) {
                        st.insert(c);
                        if (cost + 1 <= 3) {
                            q.push({c, cost + 1});
                        }
                    }
                    reverse(c.begin() + i, c.begin() + j + 1);
                }
            }
        }
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 700
memory_kb: 63204
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define all(x) x.begin(), x.end()
template <class A, class B>
inline bool chmin(A &a, const B &b) {
    return b < a && (a = b, true);
}
template <class A, class B>
inline bool chmax(A &a, const B &b) {
    return b > a && (a = b, true);
}

int main() {
    int n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i], a[i]--;
    constexpr int N = 4;
    constexpr int MASK = 0b1111;
    auto encode = [&](const vector<ll> &b) {
        ll ans = 0;
        rep(i, n) {
            ans |= b[i] << (N * i);
        }
        return ans;
    };
    auto rev = [&](ll x, int l, int r) {
        ll ans = 0;
        for (int i = 0; i < l; i++) {
            ll mask = (x >> (N * i)) & MASK;
            ans |= mask << (N * i);
        }
        for (int i = l; i < r; i++) {
            ll mask = (x >> (N * i)) & MASK;
            ans |= mask << (N * (r - 1 - (i - l)));
        }
        for (int i = r; i < n; i++) {
            ll mask = (x >> (N * i)) & MASK;
            ans |= mask << (N * i);
        }
        return ans;
    };
    auto h = [&](ll x) {
        int count = 1;
        rep(i, n) {
            ll mask = x >> (N * i) & MASK;
            count += mask != i;
        }
        return count;
    };
    ll start = encode(a);
    ll goal;
    {
        vector<ll> b(n);
        iota(all(b), 0);
        goal = encode(b);
    }
    map<ll, int> dist;
    dist[start] = 0;
    using P = pair<int, ll>;
    priority_queue<P, vector<P>, greater<P>> que;
    que.emplace(h(start), start);
    while (!que.empty()) {
        auto [w, u] = que.top();
        que.pop();
        if (u == goal) break;
        for (int l = 0; l < n; l++) {
            for (int r = l + 1; r <= n; r++) {
                ll v = rev(u, l, r);
                if (dist.find(v) == dist.end() || dist[v] > dist[u] + 1) {
                    dist[v] = dist[u] + 1;
                    que.emplace(h(v) + dist[v], v);
                }
            }
        }
    }
    cout << dist[goal] << '\n';
}
accuracy: 0.12121212121212122
time_ms: 20
memory_kb: 7732
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define all(x) x.begin(), x.end()
template <class A, class B>
inline bool chmin(A &a, const B &b) {
    return b < a && (a = b, true);
}
template <class A, class B>
inline bool chmax(A &a, const B &b) {
    return b > a && (a = b, true);
}

int main() {
    int n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i], a[i]--;
    constexpr int N = 4;
    constexpr int MASK = 0b1111;
    auto encode = [&](const vector<ll> &b) {
        ll ans = 0;
        rep(i, n) {
            ans |= b[i] << (N * i);
        }
        return ans;
    };
    auto rev = [&](ll x, int l, int r) {
        ll ans = 0;
        for (int i = 0; i < l; i++) {
            ll mask = (x >> (N * i)) & MASK;
            ans |= mask << (N * i);
        }
        for (int i = l; i < r; i++) {
            ll mask = (x >> (N * i)) & MASK;
            ans |= mask << (N * (r - 1 - (i - l)));
        }
        for (int i = r; i < n; i++) {
            ll mask = (x >> (N * i)) & MASK;
            ans |= mask << (N * i);
        }
        return ans;
    };
    auto h = [&](ll x) {
        int count = 0;
        rep(i, n) {
            ll mask = x >> (N * i) & MASK;
            count += mask != i;
        }
        return count;
    };
    ll start = encode(a);
    ll goal;
    {
        vector<ll> b(n);
        iota(all(b), 0);
        goal = encode(b);
    }
    map<ll, int> dist;
    dist[start] = 0;
    using P = pair<int, ll>;
    priority_queue<P, vector<P>, greater<P>> que;
    que.emplace(h(start), start);
    while (!que.empty()) {
        auto [w, u] = que.top();
        que.pop();
        if (u == goal) break;
        for (int l = 0; l < n; l++) {
            for (int r = l + 1; r <= n; r++) {
                ll v = rev(u, l, r);
                if (dist.find(v) == dist.end() || dist[v] > dist[u] + 1) {
                    dist[v] = dist[u] + 1;
                    que.emplace(h(v) + dist[v], v);
                }
            }
        }
    }
    cout << dist[goal] << '\n';
}
accuracy: 0.12121212121212122
time_ms: 30
memory_kb: 7684
final_rank: 18

code_snippet: //Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
#include <cassert>
#include <iomanip>
#include <chrono>
#include <random>
#include <bitset>
#include <complex>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#include <atcoder/convolution>
//#include <atcoder/lazysegtree>
//#include <atcoder/maxflow>
//#include <atcoder/mincostflow>
//#include <atcoder/segtree>
//#include <atcoder/string>
using namespace std;
//using namespace atcoder;
#define int long long
//#define L __int128
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define gnr(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define per(i,b) gnr(i,0,b)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) (int)(x.size())
#define pcnt(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()

#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
 
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
 
template<class t> using vc=vector<t>;
 
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}
 
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}
 
 //https://codeforces.com/blog/entry/62393
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
 
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
	//don't make x negative!
	size_t operator()(pair<int,int> x)const{
		return operator()(uint64_t(x.first)<<32|x.second);
	}
};
//unordered_set -> dtype, null_type
//unordered_map -> dtype(key), dtype(value)
using namespace __gnu_pbds;
template<class t,class u>
using hash_table=gp_hash_table<t,u,custom_hash>;

template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
//const ll mod = 1000000007;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

struct mint{
	ll v;
	mint(ll vv=0){s(vv%mod+mod);}
	mint& s(ll vv){
		v=vv<mod?vv:vv-mod;
		return *this;
	}
	mint operator-()const{return mint()-*this;}
	mint &operator+=(const mint&rhs){return s(v+rhs.v);}
	mint &operator-=(const mint&rhs){return s(v+mod-rhs.v);}
	mint &operator*=(const mint&rhs){v=ll(v)*rhs.v%mod;return *this;}
	mint &operator/=(const mint&rhs){return *this*=rhs.inv();}
	mint operator+(const mint&rhs)const{return mint(*this)+=rhs;}
	mint operator-(const mint&rhs)const{return mint(*this)-=rhs;}
	mint operator*(const mint&rhs)const{return mint(*this)*=rhs;}
	mint operator/(const mint&rhs)const{return mint(*this)/=rhs;}
	mint pow(ll n)const{
		if(n<0)return inv().pow(-n);
		mint res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	mint inv()const{return pow(mod-2);}
	friend mint operator+(ll x,const mint&y){
		return mint(x)+y;
	}
	friend mint operator-(ll x,const mint&y){
		return mint(x)-y;
	}
	friend mint operator*(ll x,const mint&y){
		return mint(x)*y;
	}
	friend mint operator/(ll x,const mint&y){
		return mint(x)/y;
	}
	friend ostream& operator<<(ostream&os,const mint&m){
		return os<<m.v;
	}
	friend istream& operator>>(istream&is,mint&m){
		ll x;is>>x;
		m=mint(x);
		return is;
	}
	bool operator<(const mint&r)const{return v<r.v;}
	bool operator==(const mint&r)const{return v==r.v;}
	bool operator!=(const mint&r)const{return v!=r.v;}
	explicit operator bool()const{
		return v;
	}
};
ll modpow(ll x,ll n,ll _md=mod){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%_md;
		x=x*x%_md;
		n>>=1;
	}
	return res;
}
#define _sz 1
mint F[_sz],R[_sz];
void make(){
	F[0] = 1;
	for(int i=1;i<_sz;i++) F[i] = F[i-1] * i;
	R[_sz-1] = F[_sz-1].pow(mod-2);
	for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1);
}
mint C(int a,int b){
	if(b < 0 || a < b) return mint();
	return F[a]*R[b]*R[a-b];
}
using ld=long double;
using vi=vc<int>;
using ull=unsigned long long;
/*int dst(P p, P q){
	return (p.a-q.a)*(p.a-q.a)+(p.b-q.b)*(p.b-q.b);
}*/
/*template<class t>
void add_inplace(t &a, t b){
	if(a.size() < b.size()) swap(a, b);
	for(int i=0;i<si(b);i++){
		a[i] += b[i];
		if(a[i] < 0) a[i] += mod;
		if(a[i] >= mod) a[i] -= mod;
	}
	return ;
}*/
template<class t>
void ov(const vc<t>&a){
	if(a.empty()) return;
	rep(i, si(a)) cout << a[i] << (i+1==si(a)?'\n':' ');
}
//o(ans?"Yes":"No");

void solve(){
	int n;
	cin>>n;
	vi a(n);
	rep(i,n){
		cin>>a[i];a[i]--;
	}
	vc<vi>cur{a}, nxt;
	vi g(n);
	rep(i,n) g[i]=i;
	constexpr int B = 1500;
	rep(_, INF){
		vc<vi>buf[55];
		for(auto e:cur){
			if(e == g){
				o(_);
				return;
			}
			rep(i,n-1){
				rng(j,i+1,n){
					//[i,j]
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
					int f = 0;
					rep(a,n) rng(b,a+1,n){
						f += (e[a] > e[b]);
					}
					int up = 0;
					rep(i,f) up += si(buf[i]);
					if(up < B){
						buf[f].pb(e);
					}
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
				}
			}
		}
		nxt.clear();
		rep(i,55){
			if(si(nxt)+si(buf[i]) < B){
				nxt.insert(nxt.end(), all(buf[i]));
			}
			else{
				shuffle(all(buf[i]), mt);
				int idx = 0;
				while(nxt.size() < B){
					nxt.pb(buf[i][idx++]);
				}
				break;
			}
		}
		swap(cur, nxt);
	}
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}
accuracy: 1
time_ms: 20
memory_kb: 5620
final_rank: 1

code_snippet: //Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
#include <cassert>
#include <iomanip>
#include <chrono>
#include <random>
#include <bitset>
#include <complex>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#include <atcoder/convolution>
//#include <atcoder/lazysegtree>
//#include <atcoder/maxflow>
//#include <atcoder/mincostflow>
//#include <atcoder/segtree>
//#include <atcoder/string>
using namespace std;
//using namespace atcoder;
#define int long long
//#define L __int128
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define gnr(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define per(i,b) gnr(i,0,b)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) (int)(x.size())
#define pcnt(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()

#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
 
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
 
template<class t> using vc=vector<t>;
 
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}
 
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}
 
 //https://codeforces.com/blog/entry/62393
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
 
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
	//don't make x negative!
	size_t operator()(pair<int,int> x)const{
		return operator()(uint64_t(x.first)<<32|x.second);
	}
};
//unordered_set -> dtype, null_type
//unordered_map -> dtype(key), dtype(value)
using namespace __gnu_pbds;
template<class t,class u>
using hash_table=gp_hash_table<t,u,custom_hash>;

template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
//const ll mod = 1000000007;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

struct mint{
	ll v;
	mint(ll vv=0){s(vv%mod+mod);}
	mint& s(ll vv){
		v=vv<mod?vv:vv-mod;
		return *this;
	}
	mint operator-()const{return mint()-*this;}
	mint &operator+=(const mint&rhs){return s(v+rhs.v);}
	mint &operator-=(const mint&rhs){return s(v+mod-rhs.v);}
	mint &operator*=(const mint&rhs){v=ll(v)*rhs.v%mod;return *this;}
	mint &operator/=(const mint&rhs){return *this*=rhs.inv();}
	mint operator+(const mint&rhs)const{return mint(*this)+=rhs;}
	mint operator-(const mint&rhs)const{return mint(*this)-=rhs;}
	mint operator*(const mint&rhs)const{return mint(*this)*=rhs;}
	mint operator/(const mint&rhs)const{return mint(*this)/=rhs;}
	mint pow(ll n)const{
		if(n<0)return inv().pow(-n);
		mint res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	mint inv()const{return pow(mod-2);}
	friend mint operator+(ll x,const mint&y){
		return mint(x)+y;
	}
	friend mint operator-(ll x,const mint&y){
		return mint(x)-y;
	}
	friend mint operator*(ll x,const mint&y){
		return mint(x)*y;
	}
	friend mint operator/(ll x,const mint&y){
		return mint(x)/y;
	}
	friend ostream& operator<<(ostream&os,const mint&m){
		return os<<m.v;
	}
	friend istream& operator>>(istream&is,mint&m){
		ll x;is>>x;
		m=mint(x);
		return is;
	}
	bool operator<(const mint&r)const{return v<r.v;}
	bool operator==(const mint&r)const{return v==r.v;}
	bool operator!=(const mint&r)const{return v!=r.v;}
	explicit operator bool()const{
		return v;
	}
};
ll modpow(ll x,ll n,ll _md=mod){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%_md;
		x=x*x%_md;
		n>>=1;
	}
	return res;
}
#define _sz 1
mint F[_sz],R[_sz];
void make(){
	F[0] = 1;
	for(int i=1;i<_sz;i++) F[i] = F[i-1] * i;
	R[_sz-1] = F[_sz-1].pow(mod-2);
	for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1);
}
mint C(int a,int b){
	if(b < 0 || a < b) return mint();
	return F[a]*R[b]*R[a-b];
}
using ld=long double;
using vi=vc<int>;
using ull=unsigned long long;
/*int dst(P p, P q){
	return (p.a-q.a)*(p.a-q.a)+(p.b-q.b)*(p.b-q.b);
}*/
/*template<class t>
void add_inplace(t &a, t b){
	if(a.size() < b.size()) swap(a, b);
	for(int i=0;i<si(b);i++){
		a[i] += b[i];
		if(a[i] < 0) a[i] += mod;
		if(a[i] >= mod) a[i] -= mod;
	}
	return ;
}*/
template<class t>
void ov(const vc<t>&a){
	if(a.empty()) return;
	rep(i, si(a)) cout << a[i] << (i+1==si(a)?'\n':' ');
}
//o(ans?"Yes":"No");

void solve(){
	int n;
	cin>>n;
	vi a(n);
	rep(i,n){
		cin>>a[i];a[i]--;
	}
	vc<vi>cur{a}, nxt;
	vi g(n);
	rep(i,n) g[i]=i;
	constexpr int B = 3000;
	rep(_, INF){
		vc<vi>buf[55];
		for(auto e:cur){
			if(e == g){
				o(_);
				return;
			}
			rep(i,n-1){
				rng(j,i+1,n){
					//[i,j]
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
					int f = 0;
					rep(a,n) rng(b,a+1,n){
						f += (e[a] > e[b]);
					}
					int up = 0;
					rep(i,f) up += si(buf[i]);
					if(up < B){
						buf[f].pb(e);
					}
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
				}
			}
		}
		nxt.clear();
		rep(i,55){
			if(si(nxt)+si(buf[i]) < B){
				nxt.insert(nxt.end(), all(buf[i]));
			}
			else{
				shuffle(all(buf[i]), mt);
				int idx = 0;
				while(nxt.size() < B){
					nxt.pb(buf[i][idx++]);
				}
				break;
			}
		}
		swap(cur, nxt);
	}
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}
accuracy: 1
time_ms: 40
memory_kb: 8340
final_rank: 2

code_snippet: //Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
#include <cassert>
#include <iomanip>
#include <chrono>
#include <random>
#include <bitset>
#include <complex>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#include <atcoder/convolution>
//#include <atcoder/lazysegtree>
//#include <atcoder/maxflow>
//#include <atcoder/mincostflow>
//#include <atcoder/segtree>
//#include <atcoder/string>
using namespace std;
//using namespace atcoder;
#define int long long
//#define L __int128
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define gnr(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define per(i,b) gnr(i,0,b)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) (int)(x.size())
#define pcnt(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()

#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
 
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
 
template<class t> using vc=vector<t>;
 
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}
 
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}
 
 //https://codeforces.com/blog/entry/62393
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
 
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
	//don't make x negative!
	size_t operator()(pair<int,int> x)const{
		return operator()(uint64_t(x.first)<<32|x.second);
	}
};
//unordered_set -> dtype, null_type
//unordered_map -> dtype(key), dtype(value)
using namespace __gnu_pbds;
template<class t,class u>
using hash_table=gp_hash_table<t,u,custom_hash>;

template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
//const ll mod = 1000000007;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

struct mint{
	ll v;
	mint(ll vv=0){s(vv%mod+mod);}
	mint& s(ll vv){
		v=vv<mod?vv:vv-mod;
		return *this;
	}
	mint operator-()const{return mint()-*this;}
	mint &operator+=(const mint&rhs){return s(v+rhs.v);}
	mint &operator-=(const mint&rhs){return s(v+mod-rhs.v);}
	mint &operator*=(const mint&rhs){v=ll(v)*rhs.v%mod;return *this;}
	mint &operator/=(const mint&rhs){return *this*=rhs.inv();}
	mint operator+(const mint&rhs)const{return mint(*this)+=rhs;}
	mint operator-(const mint&rhs)const{return mint(*this)-=rhs;}
	mint operator*(const mint&rhs)const{return mint(*this)*=rhs;}
	mint operator/(const mint&rhs)const{return mint(*this)/=rhs;}
	mint pow(ll n)const{
		if(n<0)return inv().pow(-n);
		mint res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	mint inv()const{return pow(mod-2);}
	friend mint operator+(ll x,const mint&y){
		return mint(x)+y;
	}
	friend mint operator-(ll x,const mint&y){
		return mint(x)-y;
	}
	friend mint operator*(ll x,const mint&y){
		return mint(x)*y;
	}
	friend mint operator/(ll x,const mint&y){
		return mint(x)/y;
	}
	friend ostream& operator<<(ostream&os,const mint&m){
		return os<<m.v;
	}
	friend istream& operator>>(istream&is,mint&m){
		ll x;is>>x;
		m=mint(x);
		return is;
	}
	bool operator<(const mint&r)const{return v<r.v;}
	bool operator==(const mint&r)const{return v==r.v;}
	bool operator!=(const mint&r)const{return v!=r.v;}
	explicit operator bool()const{
		return v;
	}
};
ll modpow(ll x,ll n,ll _md=mod){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%_md;
		x=x*x%_md;
		n>>=1;
	}
	return res;
}
#define _sz 1
mint F[_sz],R[_sz];
void make(){
	F[0] = 1;
	for(int i=1;i<_sz;i++) F[i] = F[i-1] * i;
	R[_sz-1] = F[_sz-1].pow(mod-2);
	for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1);
}
mint C(int a,int b){
	if(b < 0 || a < b) return mint();
	return F[a]*R[b]*R[a-b];
}
using ld=long double;
using vi=vc<int>;
using ull=unsigned long long;
/*int dst(P p, P q){
	return (p.a-q.a)*(p.a-q.a)+(p.b-q.b)*(p.b-q.b);
}*/
/*template<class t>
void add_inplace(t &a, t b){
	if(a.size() < b.size()) swap(a, b);
	for(int i=0;i<si(b);i++){
		a[i] += b[i];
		if(a[i] < 0) a[i] += mod;
		if(a[i] >= mod) a[i] -= mod;
	}
	return ;
}*/
template<class t>
void ov(const vc<t>&a){
	if(a.empty()) return;
	rep(i, si(a)) cout << a[i] << (i+1==si(a)?'\n':' ');
}
//o(ans?"Yes":"No");

void solve(){
	int n;
	cin>>n;
	vi a(n);
	rep(i,n){
		cin>>a[i];a[i]--;
	}
	vc<vi>cur{a}, nxt;
	vi g(n);
	rep(i,n) g[i]=i;
	constexpr int B = 5000;
	rep(_, INF){
		vc<vi>buf[55];
		for(auto e:cur){
			if(e == g){
				o(_);
				return;
			}
			rep(i,n-1){
				rng(j,i+1,n){
					//[i,j]
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
					int f = 0;
					rep(a,n) rng(b,a+1,n){
						f += (e[a] > e[b]);
					}
					int up = 0;
					rep(i,f) up += si(buf[i]);
					if(up < B){
						buf[f].pb(e);
					}
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
				}
			}
		}
		nxt.clear();
		rep(i,55){
			if(si(nxt)+si(buf[i]) < B){
				nxt.insert(nxt.end(), all(buf[i]));
			}
			else{
				shuffle(all(buf[i]), mt);
				int idx = 0;
				while(nxt.size() < B){
					nxt.pb(buf[i][idx++]);
				}
				break;
			}
		}
		swap(cur, nxt);
	}
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}
accuracy: 1
time_ms: 60
memory_kb: 11548
final_rank: 3

code_snippet: //Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
#include <cassert>
#include <iomanip>
#include <chrono>
#include <random>
#include <bitset>
#include <complex>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#include <atcoder/convolution>
//#include <atcoder/lazysegtree>
//#include <atcoder/maxflow>
//#include <atcoder/mincostflow>
//#include <atcoder/segtree>
//#include <atcoder/string>
using namespace std;
//using namespace atcoder;
#define int long long
//#define L __int128
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define gnr(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define per(i,b) gnr(i,0,b)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) (int)(x.size())
#define pcnt(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()

#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
 
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
 
template<class t> using vc=vector<t>;
 
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}
 
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}
 
 //https://codeforces.com/blog/entry/62393
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
 
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
	//don't make x negative!
	size_t operator()(pair<int,int> x)const{
		return operator()(uint64_t(x.first)<<32|x.second);
	}
};
//unordered_set -> dtype, null_type
//unordered_map -> dtype(key), dtype(value)
using namespace __gnu_pbds;
template<class t,class u>
using hash_table=gp_hash_table<t,u,custom_hash>;

template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
//const ll mod = 1000000007;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

struct mint{
	ll v;
	mint(ll vv=0){s(vv%mod+mod);}
	mint& s(ll vv){
		v=vv<mod?vv:vv-mod;
		return *this;
	}
	mint operator-()const{return mint()-*this;}
	mint &operator+=(const mint&rhs){return s(v+rhs.v);}
	mint &operator-=(const mint&rhs){return s(v+mod-rhs.v);}
	mint &operator*=(const mint&rhs){v=ll(v)*rhs.v%mod;return *this;}
	mint &operator/=(const mint&rhs){return *this*=rhs.inv();}
	mint operator+(const mint&rhs)const{return mint(*this)+=rhs;}
	mint operator-(const mint&rhs)const{return mint(*this)-=rhs;}
	mint operator*(const mint&rhs)const{return mint(*this)*=rhs;}
	mint operator/(const mint&rhs)const{return mint(*this)/=rhs;}
	mint pow(ll n)const{
		if(n<0)return inv().pow(-n);
		mint res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	mint inv()const{return pow(mod-2);}
	friend mint operator+(ll x,const mint&y){
		return mint(x)+y;
	}
	friend mint operator-(ll x,const mint&y){
		return mint(x)-y;
	}
	friend mint operator*(ll x,const mint&y){
		return mint(x)*y;
	}
	friend mint operator/(ll x,const mint&y){
		return mint(x)/y;
	}
	friend ostream& operator<<(ostream&os,const mint&m){
		return os<<m.v;
	}
	friend istream& operator>>(istream&is,mint&m){
		ll x;is>>x;
		m=mint(x);
		return is;
	}
	bool operator<(const mint&r)const{return v<r.v;}
	bool operator==(const mint&r)const{return v==r.v;}
	bool operator!=(const mint&r)const{return v!=r.v;}
	explicit operator bool()const{
		return v;
	}
};
ll modpow(ll x,ll n,ll _md=mod){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%_md;
		x=x*x%_md;
		n>>=1;
	}
	return res;
}
#define _sz 1
mint F[_sz],R[_sz];
void make(){
	F[0] = 1;
	for(int i=1;i<_sz;i++) F[i] = F[i-1] * i;
	R[_sz-1] = F[_sz-1].pow(mod-2);
	for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1);
}
mint C(int a,int b){
	if(b < 0 || a < b) return mint();
	return F[a]*R[b]*R[a-b];
}
using ld=long double;
using vi=vc<int>;
using ull=unsigned long long;
/*int dst(P p, P q){
	return (p.a-q.a)*(p.a-q.a)+(p.b-q.b)*(p.b-q.b);
}*/
/*template<class t>
void add_inplace(t &a, t b){
	if(a.size() < b.size()) swap(a, b);
	for(int i=0;i<si(b);i++){
		a[i] += b[i];
		if(a[i] < 0) a[i] += mod;
		if(a[i] >= mod) a[i] -= mod;
	}
	return ;
}*/
template<class t>
void ov(const vc<t>&a){
	if(a.empty()) return;
	rep(i, si(a)) cout << a[i] << (i+1==si(a)?'\n':' ');
}
//o(ans?"Yes":"No");

void solve(){
	int n;
	cin>>n;
	vi a(n);
	rep(i,n){
		cin>>a[i];a[i]--;
	}
	vc<vi>cur{a}, nxt;
	vi g(n);
	rep(i,n) g[i]=i;
	constexpr int B = 10000;
	rep(_, INF){
		vc<vi>buf[55];
		for(auto e:cur){
			if(e == g){
				o(_);
				return;
			}
			rep(i,n-1){
				rng(j,i+1,n){
					//[i,j]
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
					int f = 0;
					rep(a,n) rng(b,a+1,n){
						f += (e[a] > e[b]);
					}
					int up = 0;
					rep(i,f) up += si(buf[i]);
					if(up < B){
						buf[f].pb(e);
					}
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
				}
			}
		}
		nxt.clear();
		rep(i,55){
			if(si(nxt)+si(buf[i]) < B){
				nxt.insert(nxt.end(), all(buf[i]));
			}
			else{
				shuffle(all(buf[i]), mt);
				int idx = 0;
				while(nxt.size() < B){
					nxt.pb(buf[i][idx++]);
				}
				break;
			}
		}
		swap(cur, nxt);
	}
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}
accuracy: 1
time_ms: 130
memory_kb: 20224
final_rank: 4

code_snippet: //Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
#include <cassert>
#include <iomanip>
#include <chrono>
#include <random>
#include <bitset>
#include <complex>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#include <atcoder/convolution>
//#include <atcoder/lazysegtree>
//#include <atcoder/maxflow>
//#include <atcoder/mincostflow>
//#include <atcoder/segtree>
//#include <atcoder/string>
using namespace std;
//using namespace atcoder;
#define int long long
//#define L __int128
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define gnr(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define per(i,b) gnr(i,0,b)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) (int)(x.size())
#define pcnt(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()

#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
 
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
 
template<class t> using vc=vector<t>;
 
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}
 
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}
 
 //https://codeforces.com/blog/entry/62393
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
 
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
	//don't make x negative!
	size_t operator()(pair<int,int> x)const{
		return operator()(uint64_t(x.first)<<32|x.second);
	}
};
//unordered_set -> dtype, null_type
//unordered_map -> dtype(key), dtype(value)
using namespace __gnu_pbds;
template<class t,class u>
using hash_table=gp_hash_table<t,u,custom_hash>;

template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
//const ll mod = 1000000007;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

struct mint{
	ll v;
	mint(ll vv=0){s(vv%mod+mod);}
	mint& s(ll vv){
		v=vv<mod?vv:vv-mod;
		return *this;
	}
	mint operator-()const{return mint()-*this;}
	mint &operator+=(const mint&rhs){return s(v+rhs.v);}
	mint &operator-=(const mint&rhs){return s(v+mod-rhs.v);}
	mint &operator*=(const mint&rhs){v=ll(v)*rhs.v%mod;return *this;}
	mint &operator/=(const mint&rhs){return *this*=rhs.inv();}
	mint operator+(const mint&rhs)const{return mint(*this)+=rhs;}
	mint operator-(const mint&rhs)const{return mint(*this)-=rhs;}
	mint operator*(const mint&rhs)const{return mint(*this)*=rhs;}
	mint operator/(const mint&rhs)const{return mint(*this)/=rhs;}
	mint pow(ll n)const{
		if(n<0)return inv().pow(-n);
		mint res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	mint inv()const{return pow(mod-2);}
	friend mint operator+(ll x,const mint&y){
		return mint(x)+y;
	}
	friend mint operator-(ll x,const mint&y){
		return mint(x)-y;
	}
	friend mint operator*(ll x,const mint&y){
		return mint(x)*y;
	}
	friend mint operator/(ll x,const mint&y){
		return mint(x)/y;
	}
	friend ostream& operator<<(ostream&os,const mint&m){
		return os<<m.v;
	}
	friend istream& operator>>(istream&is,mint&m){
		ll x;is>>x;
		m=mint(x);
		return is;
	}
	bool operator<(const mint&r)const{return v<r.v;}
	bool operator==(const mint&r)const{return v==r.v;}
	bool operator!=(const mint&r)const{return v!=r.v;}
	explicit operator bool()const{
		return v;
	}
};
ll modpow(ll x,ll n,ll _md=mod){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%_md;
		x=x*x%_md;
		n>>=1;
	}
	return res;
}
#define _sz 1
mint F[_sz],R[_sz];
void make(){
	F[0] = 1;
	for(int i=1;i<_sz;i++) F[i] = F[i-1] * i;
	R[_sz-1] = F[_sz-1].pow(mod-2);
	for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1);
}
mint C(int a,int b){
	if(b < 0 || a < b) return mint();
	return F[a]*R[b]*R[a-b];
}
using ld=long double;
using vi=vc<int>;
using ull=unsigned long long;
/*int dst(P p, P q){
	return (p.a-q.a)*(p.a-q.a)+(p.b-q.b)*(p.b-q.b);
}*/
/*template<class t>
void add_inplace(t &a, t b){
	if(a.size() < b.size()) swap(a, b);
	for(int i=0;i<si(b);i++){
		a[i] += b[i];
		if(a[i] < 0) a[i] += mod;
		if(a[i] >= mod) a[i] -= mod;
	}
	return ;
}*/
template<class t>
void ov(const vc<t>&a){
	if(a.empty()) return;
	rep(i, si(a)) cout << a[i] << (i+1==si(a)?'\n':' ');
}
//o(ans?"Yes":"No");

void solve(){
	int n;
	cin>>n;
	vi a(n);
	rep(i,n){
		cin>>a[i];a[i]--;
	}
	vc<vi>cur{a}, nxt;
	vi g(n);
	rep(i,n) g[i]=i;
	constexpr int B = 1000;
	rep(_, INF){
		vc<vi>buf[55];
		for(auto e:cur){
			if(e == g){
				o(_);
				return;
			}
			rep(i,n-1){
				rng(j,i+1,n){
					//[i,j]
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
					int f = 0;
					rep(a,n) rng(b,a+1,n){
						f += (e[a] > e[b]);
					}
					int up = 0;
					rep(i,f) up += si(buf[i]);
					if(up < B){
						buf[f].pb(e);
					}
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
				}
			}
		}
		nxt.clear();
		rep(i,55){
			if(si(nxt)+si(buf[i]) < B){
				nxt.insert(nxt.end(), all(buf[i]));
			}
			else{
				shuffle(all(buf[i]), mt);
				int idx = 0;
				while(nxt.size() < B){
					nxt.pb(buf[i][idx++]);
				}
				break;
			}
		}
		swap(cur, nxt);
	}
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}
accuracy: 0.8787878787878788
time_ms: 10
memory_kb: 4980
final_rank: 16

code_snippet: //Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
#include <cassert>
#include <iomanip>
#include <chrono>
#include <random>
#include <bitset>
#include <complex>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#include <atcoder/convolution>
//#include <atcoder/lazysegtree>
//#include <atcoder/maxflow>
//#include <atcoder/mincostflow>
//#include <atcoder/segtree>
//#include <atcoder/string>
using namespace std;
//using namespace atcoder;
#define int long long
//#define L __int128
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define gnr(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define per(i,b) gnr(i,0,b)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) (int)(x.size())
#define pcnt(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()

#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
 
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
 
template<class t> using vc=vector<t>;
 
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}
 
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}
 
 //https://codeforces.com/blog/entry/62393
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
 
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
	//don't make x negative!
	size_t operator()(pair<int,int> x)const{
		return operator()(uint64_t(x.first)<<32|x.second);
	}
};
//unordered_set -> dtype, null_type
//unordered_map -> dtype(key), dtype(value)
using namespace __gnu_pbds;
template<class t,class u>
using hash_table=gp_hash_table<t,u,custom_hash>;

template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
//const ll mod = 1000000007;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

struct mint{
	ll v;
	mint(ll vv=0){s(vv%mod+mod);}
	mint& s(ll vv){
		v=vv<mod?vv:vv-mod;
		return *this;
	}
	mint operator-()const{return mint()-*this;}
	mint &operator+=(const mint&rhs){return s(v+rhs.v);}
	mint &operator-=(const mint&rhs){return s(v+mod-rhs.v);}
	mint &operator*=(const mint&rhs){v=ll(v)*rhs.v%mod;return *this;}
	mint &operator/=(const mint&rhs){return *this*=rhs.inv();}
	mint operator+(const mint&rhs)const{return mint(*this)+=rhs;}
	mint operator-(const mint&rhs)const{return mint(*this)-=rhs;}
	mint operator*(const mint&rhs)const{return mint(*this)*=rhs;}
	mint operator/(const mint&rhs)const{return mint(*this)/=rhs;}
	mint pow(ll n)const{
		if(n<0)return inv().pow(-n);
		mint res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	mint inv()const{return pow(mod-2);}
	friend mint operator+(ll x,const mint&y){
		return mint(x)+y;
	}
	friend mint operator-(ll x,const mint&y){
		return mint(x)-y;
	}
	friend mint operator*(ll x,const mint&y){
		return mint(x)*y;
	}
	friend mint operator/(ll x,const mint&y){
		return mint(x)/y;
	}
	friend ostream& operator<<(ostream&os,const mint&m){
		return os<<m.v;
	}
	friend istream& operator>>(istream&is,mint&m){
		ll x;is>>x;
		m=mint(x);
		return is;
	}
	bool operator<(const mint&r)const{return v<r.v;}
	bool operator==(const mint&r)const{return v==r.v;}
	bool operator!=(const mint&r)const{return v!=r.v;}
	explicit operator bool()const{
		return v;
	}
};
ll modpow(ll x,ll n,ll _md=mod){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%_md;
		x=x*x%_md;
		n>>=1;
	}
	return res;
}
#define _sz 1
mint F[_sz],R[_sz];
void make(){
	F[0] = 1;
	for(int i=1;i<_sz;i++) F[i] = F[i-1] * i;
	R[_sz-1] = F[_sz-1].pow(mod-2);
	for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1);
}
mint C(int a,int b){
	if(b < 0 || a < b) return mint();
	return F[a]*R[b]*R[a-b];
}
using ld=long double;
using vi=vc<int>;
using ull=unsigned long long;
/*int dst(P p, P q){
	return (p.a-q.a)*(p.a-q.a)+(p.b-q.b)*(p.b-q.b);
}*/
/*template<class t>
void add_inplace(t &a, t b){
	if(a.size() < b.size()) swap(a, b);
	for(int i=0;i<si(b);i++){
		a[i] += b[i];
		if(a[i] < 0) a[i] += mod;
		if(a[i] >= mod) a[i] -= mod;
	}
	return ;
}*/
template<class t>
void ov(const vc<t>&a){
	if(a.empty()) return;
	rep(i, si(a)) cout << a[i] << (i+1==si(a)?'\n':' ');
}
//o(ans?"Yes":"No");

void solve(){
	int n;
	cin>>n;
	vi a(n);
	rep(i,n){
		cin>>a[i];a[i]--;
	}
	vc<vi>cur{a}, nxt;
	vi g(n);
	rep(i,n) g[i]=i;
	constexpr int B = 100000;
	rep(_, INF){
		vc<vi>buf[55];
		for(auto e:cur){
			if(e == g){
				o(_);
				return;
			}
			rep(i,n-1){
				rng(j,i+1,n){
					//[i,j]
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
					int f = 0;
					rep(a,n) rng(b,a+1,n){
						f += (e[a] > e[b]);
					}
					int up = 0;
					rep(i,f) up += si(buf[i]);
					if(up < B){
						buf[f].pb(e);
					}
					rng(k,i,j+1){
						if(k >= i+j-k) break;
						swap(e[k], e[i+j-k]);
					}
				}
			}
		}
		nxt.clear();
		rep(i,55){
			if(si(nxt)+si(buf[i]) < B){
				nxt.insert(nxt.end(), all(buf[i]));
			}
			else{
				shuffle(all(buf[i]), mt);
				int idx = 0;
				while(nxt.size() < B){
					nxt.pb(buf[i][idx++]);
				}
				break;
			}
		}
		swap(cur, nxt);
	}
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}
accuracy: 1
time_ms: 1480
memory_kb: 174148
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,a;
  vector<int> s,t;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a;
    s.push_back(a);
    t.push_back(i+1);
  }
  map<vector<int>, int> m;
  queue<vector<int> > q;
  m[s]=0;
  q.push(s);
  while(!q.empty()){
    vector<int> v=q.front();
    q.pop();
    int c=m[v];
    if(c==4)continue;
    for(int i=0;i<n-1;i++){
      for(int j=i+2;j<=n;j++){
        vector<int> r=v;
        reverse(r.begin()+i,r.begin()+j);
        if(!m.count(r)){
          m[r]=c+1;
          q.push(r);
        }
      }
    }
  }
  int x=9;
  map<vector<int>, int> p;
  p[t]=0;
  q.push(t);
  while(!q.empty()){
    vector<int> v=q.front();
    q.pop();
    int c=p[v];
    if(m.count(v)) x=min(x,m[v]+c);
    if(c==2)continue;
    for(int i=0;i<n-1;i++){
      for(int j=i+2;j<=n;j++){
        vector<int> r=v;
        reverse(r.begin()+i,r.begin()+j);
        if(!p.count(r)){
          p[r]=c+1;
          q.push(r);
        }
      }
    }
  }
  cout<<x<<endl;
  return 0;
}
accuracy: 1
time_ms: 580
memory_kb: 51160
final_rank: 6

code_snippet: #include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
int main(){
  int n,a;
  vector<int> s,t;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a;
    s.push_back(a);
    t.push_back(i+1);
  }
  map<vector<int>, int> m;
  queue<vector<int> > q;
  m[s]=0;
  q.push(s);
  while(!q.empty()){
    vector<int> v=q.front();
    q.pop();
    int c=m[v];
    if(c==4)continue;
    for(int i=0;i<n-1;i++){
      for(int j=i+2;j<=n;j++){
        vector<int> r=v;
        reverse(r.begin()+i,r.begin()+j);
        if(!m.count(r)){
          m[r]=c+1;
          q.push(r);
        }
      }
    }
  }
  int x=9;
  map<vector<int>, int> p;
  queue<vector<int> > u;
  p[t]=0;
  u.push(t);
  while(!u.empty()){
    vector<int> v=u.front();
    u.pop();
    int c=p[v];
    if(m.count(v)) x=min(x,m[v]+c);
    if(c==2)continue;
    for(int i=0;i<n-1;i++){
      for(int j=i+2;j<=n;j++){
        vector<int> r=v;
        reverse(r.begin()+i,r.begin()+j);
        if(!p.count(r)){
          p[r]=c+1;
          u.push(r);
        }
      }
    }
  }
  cout<<x<<endl;
  return 0;
}
accuracy: 1
time_ms: 600
memory_kb: 51160
final_rank: 8

code_snippet: #include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
int main(){
  int n,a;
  vector<int> s,t;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a;
    s.push_back(a);
    t.push_back(i+1);
  }
  map<vector<int>, int> m;
  queue<vector<int> > q;
  m[s]=0;
  q.push(s);
  while(!q.empty()){
    vector<int> v=q.front();
    q.pop();
    int c=m[v];
    if(c==4)continue;
    for(int i=0;i<n-1;i++){
      for(int j=i+2;j<=n;j++){
        vector<int> r=v;
        reverse(r.begin()+i,r.begin()+j);
        if(!m.count(r)){
          m[r]=c+1;
          q.push(r);
        }
      }
    }
  }
  int x=9;
  map<vector<int>, int> p;
  p[t]=0;
  q.push(t);
  while(!q.empty()){
    vector<int> v=q.front();
    q.pop();
    int c=p[v];
    if(m.count(v)) x=min(x,m[v]+c);
    if(c==2)continue;
    for(int i=0;i<n-1;i++){
      for(int j=i+2;j<=n;j++){
        vector<int> r=v;
        reverse(r.begin()+i,r.begin()+j);
        if(!p.count(r)){
          p[r]=c+1;
          q.push(r);
        }
      }
    }
  }
  cout<<x<<endl;
  return 0;
}
accuracy: 1
time_ms: 590
memory_kb: 51160
final_rank: 7

code_snippet: #include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
int main(){
  int n,a,x=9;
  vector<int> s,t;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a;
    s.push_back(a-1);
    t.push_back(i);
  }
  map<vector<int>, int> m;
  queue<vector<int> > q;
  m[s]=0;
  q.push(s);
  while(!q.empty()){
    vector<int> v=q.front();
    q.pop();
    int c=m[v];
    if(c==4)continue;
    for(int i=0;i<n-1;i++){
      for(int j=i+2;j<=n;j++){
        vector<int> r=v;
        reverse(r.begin()+i,r.begin()+j);
        if(!m.count(r)){
          m[r]=c+1;
          q.push(r);
        }
      }
    }
  }
  int ans=9;
  map<vector<int>, int> p;
  queue<vector<int> > u;
  p[t]=0;
  u.push(t);
  while(!u.empty()){
    vector<int> v=u.front();
    u.pop();
    int c=p[v];
    if(m.count(v)) ans=min(ans,m[v]+c);
    if(c==4)continue;
    for(int i=0;i<n-1;i++){
      for(int j=i+2;j<=n;j++){
        vector<int> r=v;
        reverse(r.begin()+i,r.begin()+j);
        if(!m.count(r)){
          p[r]=c+1;
          u.push(r);
        }
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}
accuracy: 0.12121212121212122
time_ms: 2660
memory_kb: 150636
final_rank: 20

code_snippet: #include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
map<vector<int>, int> m,p;
queue<vector<int> > q;
int main(){
  int n,a,x=9;
  vector<int> s,t;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a;
    s.push_back(a-1);
    t.push_back(i);
  }
  m[s]=0;
  q.push(s);
  while(!q.empty()){
    vector<int> v=q.front();
    q.pop();
    int c=m[v];
    if(c==4)continue;
    for(int i=0;i<n-1;i++){
      for(int j=i+2;j<=n;j++){
        vector<int> r=v;
        reverse(r.begin()+i,r.begin()+j);
        if(!m.count(r)){
          m[r]=c+1;
          q.push(r);
        }
      }
    }
  }
  int ans=9;
  p[t]=0;
  q.push(t);
  while(!q.empty()){
    vector<int> v=q.front();
    q.pop();
    int c=p[v];
    if(m.count(v)) ans=min(ans,m[v]+c);
    if(c==4)continue;
    for(int i=0;i<n-1;i++){
      for(int j=i+2;j<=n;j++){
        vector<int> r=v;
        reverse(r.begin()+i,r.begin()+j);
        if(!m.count(r)){
          p[r]=c+1;
          q.push(r);
        }
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}
accuracy: 0.12121212121212122
time_ms: 2660
memory_kb: 150512
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl '\n'
#define spa << " " << 
const ll MOD = 1000000007;
const int inf = 1 << 30;
ll pow10[12];

ll rev(ll x, int l, int r){
	for(int i = l; i < r; i++){
		int j = r - (i - l);
		if(i >= j) break;
		ll l_ = (x / pow10[i]) % 10;
		ll r_ = (x / pow10[j]) % 10;
		x -= l_ * pow10[i];
		x += r_ * pow10[i];
		x -= r_ * pow10[j];
		x += l_ * pow10[j];
	}
	return x;
}

void solve(){
	int n;
	cin >> n;
	pow10[0] = 1;
	for(int i = 1; i <= n; i++) pow10[i] = pow10[i - 1] * 10;
	ll x = 0, y = 0;
	int a;
	for(int i = 0; i < n; i++){
		cin >> a;
		a--; 
		x *= 10; x += a;
		y *= 10; y += i;
	}
	if(x == y){
		cout << 0 << endl;
		return;
	}

	auto dfs=[&](ll x){
		map<ll, int> ret;
		ret[x] = 0;
		queue<pair<int, ll>> que;
		que.push({0, x});
		while(!que.empty()){
			int d = que.front().first;
			ll y = que.front().second;
			que.pop();
			for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
				ll z = rev(y, i, j);
				if(!ret.count(z)){
					ret[z] = d + 1;
					if(d <= 2) que.push({d + 1, z});
				}
			}
		}
		return ret;
	};

	auto L = dfs(x);
	auto R = dfs(y);
	int ans = 9;
	for(auto tmp:L){
		ll z = tmp.first;
		if(R.count(z)) ans = min(ans, L[z] + R[z]);
	}
	cout << ans << endl;
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cout << fixed << setprecision(12);
	int t;
	t = 1;
	while(t--) solve();
}
accuracy: 1
time_ms: 460
memory_kb: 35244
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < (int) v.size(); ++i) {
        os << v[i];
        if (i < (int) v.size() - 1) os << " ";
    }
    return os;
}

int dist1[1<<23];
int dist2[1<<23];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;
    vector<int> A(N);
    rep(i,0,N) cin >> A[i], --A[i];

    vector<int> factorial(N+1, 1);
    rep(i,1,N+1) factorial[i] = i*factorial[i-1];

    auto index = [&](auto& v) {
        int res = 0;
        rep(i,0,N) {
            int cnt = 0;
            rep(j,i+1,N) if (v[j] < v[i]) ++cnt;
            res += cnt*factorial[N-i-1];
        }
        return res;
    };

    auto get_perm = [&](int k) {
        vector<int> nums(N);
        iota(all(nums), 0);
        vector<int> res(N);
        rep(i,0,N) {
            int c = k/factorial[N-i-1];
            res[i] = nums[c];
            nums.erase(nums.begin()+c);
            k %= factorial[N-i-1];
        }
        return res;
    };

    memset(dist1, -1, sizeof(dist1));
    int k = index(A);
    dist1[k] = 0;

    queue<int> que;
    que.push(k);
    while (!que.empty()) {
        auto v = que.front();
        que.pop();
        int d = dist1[v];
        auto perm = get_perm(v);
        rep(i,0,N) rep(j,i+2,N+1) {
            reverse(perm.begin()+i, perm.begin()+j);
            int u = index(perm);
            if (dist1[u] == -1) {
                dist1[u] = d+1;
                if (d < 4) que.push(u);
            }
            reverse(perm.begin()+i, perm.begin()+j);
        }
    }

    int ans = 9;
    memset(dist2, -1, sizeof(dist2));
    dist2[0] = 0;

    que.push(0);
    while (!que.empty()) {
        auto v = que.front();
        que.pop();
        int d = dist2[v];
        if (dist1[v] != -1) {
            chmin(ans, d+dist1[v]);
        }
        auto perm = get_perm(v);
        rep(i,0,N) rep(j,i+2,N+1) {
            reverse(perm.begin()+i, perm.begin()+j);
            int u = index(perm);
            if (dist2[u] == -1) {
                dist2[u] = d+1;
                if (d < 4) que.push(u);
            }
            reverse(perm.begin()+i, perm.begin()+j);
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 2580
memory_kb: 69540
final_rank: 13