#include<bits/stdc++.h>
using namespace std;
#define ll long long int  
#define vi vector<int> 
#define vll vector<ll>
/*int gcd(int a,int b){
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}
*/
void solve(){
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int cnt=0;
    for(int i=0;i<n;i++){
        if(a[i]!=i+1)
            cnt=1;
    }
    if(a[0]==n && a[n-1]==1){
        cout<<"3"<<endl;
        return;
    }
    if(cnt==0){
        cout<<"0"<<endl;
    }
    else if(a[0]==1 || a[n-1]==n){
        cout<<"1"<<endl;
        return;
    }
    else
        cout<<"2"<<endl;

       

}
int main(){
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif 

    int n;
    cin>>n;
    while(n--){
        solve();
    }
}