#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define us unsigned
#define mod 1000000007

int main(){
    ll int test;
    cin >> test;
    while(test--){
        int n;
        cin >> n;
        vector <int> vec (n, 0);
        for (int i = 0; i < n; i++){
            cin >> vec[i];
        }
        
        int ans = 0;
        vector <bool> idx (n, false);
        int count = 0;
        
        for (int i = 0; i < vec.size(); i++){
            if (vec[i] == i+1){
                idx[i] = true;
                count++;
            }
        }
        
        if (count == n){
            cout << "0" << endl;
        }
        else if (idx[0] || idx[n-1]){
            cout << "1" << endl;
        }
        else if (vec[0] == n && vec[n-1] == 1){
            cout << "3" << endl;
        }
        else cout << "2" << endl;
    }
}