#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr(a,b) for(int i = a; i < b; i++)
#define rep(i,a,b) for(int i = a; i < b; i++)
#define mod 1000000007
#define inf (1LL<<60)
#define all(x) (x).begin(), (x).end()
#define prDouble(x) cout << fixed << setprecision(10) << x
#define triplet pair<ll,pair<ll,ll>>
#define goog(tno) cout << "Case #" << tno <<": "
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define arc(x) int x; cin >> x
using namespace std;
void init_code(){
    fast_io;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif 
} 
int main() {
   init_code();
    ll tc = 1;
    cin>>tc;
    while(tc--){
    arc(n);
    int a[n];
    bool f=0;
    for(int i=0; i<n; i++){
        cin>>a[i];
        if(a[i]!=i+1){
            f=1;
        }
    }
    if(f==0){
        cout<<0<<"\n";
    }
    else {
        if(a[0]==n && a[n-1]==1){
            cout<<3<<"\n";
        }
        else if(a[0]==1 || a[n-1]==n){
            cout<<1<<"\n";
        }
        else {
            cout<<2<<"\n";
        }
    }
  
    }
   return 0;
}