#include <bits/stdc++.h>
using namespace std;

int main() {
int t;cin>>t;
while(t--)
{
	int n;cin>>n;
	int flag=1;
	vector<int>v(n+1);
	
	for(int i=1;i<=n;i++)
	 { cin>>v[i];
	 if(v[i]!=i)flag=0;
	 }
	  if(flag) cout<<0;
	  else{
	  	if(v[1]==1||v[n]==n)
	  	cout<<1;
	  else if(v[1]==n&&v[n]!=1)
	  cout<<2;
	  else if((v[1]!=n&&v[n]==1))
	  cout<<2;
	  else if((v[1]==n&&v[n]==1))
	   cout<<3;
	   else cout<<2;
	   
	  }
	  cout<<endl;
	  
}
	return 0;
}