#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int T = 1;
int n, a[55];
int main() {
    ios_base::sync_with_stdio(0);
    cin >> T;
    while(T--) {
        cin >> n;
        int flag = 1;
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
            if (a[i] < a[i - 1]) flag = 0;
        }
        if (flag) cout << 0 << '\n';
        else if (a[1] == 1 || a[n] == n) cout << 1 << '\n';
        else if (a[1] == n && a[n] == 1) cout << 3 << '\n';
        else cout << 2 << '\n';
    }
    return 0;
}
