#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
// Hàm kiểm tra xem dãy đã được sắp xếp tăng dần hay chưa
bool isSorted(const vector<int>& arr) {
for (size_t i = 1; i < arr.size(); ++i) {
if (arr[i - 1] > arr[i]) {
return false;
}
}
return true;
}
// Hàm thực hiện BFS để tìm số bước ít nhất
int minFlipsToSort(vector<int> arr) {
int n = arr.size();
vector<int> target(n);
for (int i = 0; i < n; ++i) {
target[i] = i + 1;
}
// Nếu dãy đã sắp xếp, không cần thao tác nào
if (arr == target) {
return 0;
}
// Hàng đợi lưu trữ các trạng thái và số bước
queue<pair<vector<int>, int>> q;
q.push({arr, 0});
// Tập hợp lưu các trạng thái đã thăm
set<vector<int>> visited;
visited.insert(arr);
while (!q.empty()) {
auto [current, steps] = q.front();
q.pop();
// Thử tất cả các phép đảo ngược đoạn đầu có thể
for (int i = 1; i < n; ++i) {
vector<int> next = current;
reverse(next.begin(), next.begin() + i + 1);
// Nếu đạt được dãy mục tiêu, trả về số bước
if (next == target) {
return steps + 1;
}
// Nếu trạng thái mới chưa được thăm, thêm vào hàng đợi
if (visited.find(next) == visited.end()) {
visited.insert(next);
q.push({next, steps + 1});
}
}
}
// Trường hợp không tìm thấy (không xảy ra với n ≤ 8)
return -1;
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int& x : arr) {
cin >> x;
}
int result = minFlipsToSort(arr);
cout << result << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxhbGdvcml0aG0+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gSMOgbSBraeG7g20gdHJhIHhlbSBkw6N5IMSRw6MgxJHGsOG7o2Mgc+G6r3AgeOG6v3AgdMSDbmcgZOG6p24gaGF5IGNoxrBhCmJvb2wgaXNTb3J0ZWQoY29uc3QgdmVjdG9yPGludD4mIGFycikgewogICAgZm9yIChzaXplX3QgaSA9IDE7IGkgPCBhcnIuc2l6ZSgpOyArK2kpIHsKICAgICAgICBpZiAoYXJyW2kgLSAxXSA+IGFycltpXSkgewogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIHRydWU7Cn0KCi8vIEjDoG0gdGjhu7FjIGhp4buHbiBCRlMgxJHhu4MgdMOsbSBz4buRIGLGsOG7m2Mgw610IG5o4bqldAppbnQgbWluRmxpcHNUb1NvcnQodmVjdG9yPGludD4gYXJyKSB7CiAgICBpbnQgbiA9IGFyci5zaXplKCk7CiAgICB2ZWN0b3I8aW50PiB0YXJnZXQobik7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgICAgIHRhcmdldFtpXSA9IGkgKyAxOwogICAgfQoKICAgIC8vIE7hur91IGTDo3kgxJHDoyBz4bqvcCB44bq/cCwga2jDtG5nIGPhuqduIHRoYW8gdMOhYyBuw6BvCiAgICBpZiAoYXJyID09IHRhcmdldCkgewogICAgICAgIHJldHVybiAwOwogICAgfQoKICAgIC8vIEjDoG5nIMSR4bujaSBsxrB1IHRy4buvIGPDoWMgdHLhuqFuZyB0aMOhaSB2w6Agc+G7kSBixrDhu5tjCiAgICBxdWV1ZTxwYWlyPHZlY3RvcjxpbnQ+LCBpbnQ+PiBxOwogICAgcS5wdXNoKHthcnIsIDB9KTsKCiAgICAvLyBU4bqtcCBo4bujcCBsxrB1IGPDoWMgdHLhuqFuZyB0aMOhaSDEkcOjIHRoxINtCiAgICBzZXQ8dmVjdG9yPGludD4+IHZpc2l0ZWQ7CiAgICB2aXNpdGVkLmluc2VydChhcnIpOwoKICAgIHdoaWxlICghcS5lbXB0eSgpKSB7CiAgICAgICAgYXV0byBbY3VycmVudCwgc3RlcHNdID0gcS5mcm9udCgpOwogICAgICAgIHEucG9wKCk7CgogICAgICAgIC8vIFRo4butIHThuqV0IGPhuqMgY8OhYyBwaMOpcCDEkeG6o28gbmfGsOG7o2MgxJFv4bqhbiDEkeG6p3UgY8OzIHRo4buDCiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPCBuOyArK2kpIHsKICAgICAgICAgICAgdmVjdG9yPGludD4gbmV4dCA9IGN1cnJlbnQ7CiAgICAgICAgICAgIHJldmVyc2UobmV4dC5iZWdpbigpLCBuZXh0LmJlZ2luKCkgKyBpICsgMSk7CgogICAgICAgICAgICAvLyBO4bq/dSDEkeG6oXQgxJHGsOG7o2MgZMOjeSBt4bulYyB0acOqdSwgdHLhuqMgduG7gSBz4buRIGLGsOG7m2MKICAgICAgICAgICAgaWYgKG5leHQgPT0gdGFyZ2V0KSB7CiAgICAgICAgICAgICAgICByZXR1cm4gc3RlcHMgKyAxOwogICAgICAgICAgICB9CgogICAgICAgICAgICAvLyBO4bq/dSB0cuG6oW5nIHRow6FpIG3hu5tpIGNoxrBhIMSRxrDhu6NjIHRoxINtLCB0aMOqbSB2w6BvIGjDoG5nIMSR4bujaQogICAgICAgICAgICBpZiAodmlzaXRlZC5maW5kKG5leHQpID09IHZpc2l0ZWQuZW5kKCkpIHsKICAgICAgICAgICAgICAgIHZpc2l0ZWQuaW5zZXJ0KG5leHQpOwogICAgICAgICAgICAgICAgcS5wdXNoKHtuZXh0LCBzdGVwcyArIDF9KTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICAvLyBUcsaw4budbmcgaOG7o3Aga2jDtG5nIHTDrG0gdGjhuqV5IChraMO0bmcgeOG6o3kgcmEgduG7m2kgbiDiiaQgOCkKICAgIHJldHVybiAtMTsKfQoKaW50IG1haW4oKSB7CiAgICBpbnQgbjsKICAgIGNpbiA+PiBuOwogICAgdmVjdG9yPGludD4gYXJyKG4pOwogICAgZm9yIChpbnQmIHggOiBhcnIpIHsKICAgICAgICBjaW4gPj4geDsKICAgIH0KCiAgICBpbnQgcmVzdWx0ID0gbWluRmxpcHNUb1NvcnQoYXJyKTsKICAgIGNvdXQgPDwgcmVzdWx0IDw8IGVuZGw7CgogICAgcmV0dXJuIDA7Cn0=