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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
       int n;
       cin>>n;
       int A[n];
       for(int i=0;i<n;i++)
       {
           cin>>A[i];
       }
       bool flag=true;
       for(int i=0;i<n;i++) if(A[i]!=i+1) {flag=false; break;}
       if(flag) cout<<"0"<<endl;
       else if((A[0]==1)||(A[n-1]==n)) cout<<"1"<<endl;
       else if((A[0]==n)&&(A[n-1]==1)) cout<<"3"<<endl;
       else cout<<"2"<<endl;
    }
}