#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t,n,k,i,d,m=0;
	vector<int> a;
	cin >> t;
	for(k=0;k<t;k++)
	{
		cin >> n;
		for(i=0;i<n;i++)
		{
			cin >> d;
			a.push_back(d);
			if(a[i]!=i+1)
				m=1;
		}
		if(m==0)
			cout << "0" << "\n";

		else if(a[0]==1||a[n-1]==n)
			cout << "1" << "\n";
		else if(a[0]==n&&a[n-1]==1)
			cout << "3" << "\n";

		else 
			cout << "2" << "\n";
		m=0;
		a.clear();
		
	}
}