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

int main()
{

    ll t;
    cin >> t;

    while(t--)
    {
        ll n;
        cin >> n;

        ll a[n];

        ll c=0;
        for(int i=0;i<n;i++)
        {
            cin >> a[i];
            if(i+1-a[i]==0)
                c++;
        }

        if(c==n)
            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;
    }
    return 0;
}