using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3");
WillReturn.Add("5");
WillReturn.Add("2 1 3 5 4");
WillReturn.Add("3");
WillReturn.Add("1 2 3");
WillReturn.Add("7");
WillReturn.Add("3 2 1 7 5 6 4");
//1
//0
//2
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
var sb = new System.Text.StringBuilder();
List<string> InputList = GetInputList();
for (int I = 2; I <= InputList.Count - 1; I += 2) {
long[] PArr = InputList[I].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long Result = Solve(PArr);
sb.Append(Result);
sb.AppendLine();
}
Console.Write(sb.ToString());
}
static long Solve(long[] pArr)
{
long InvCnt1 = DeriveInvCnt(pArr);
if (InvCnt1 == 0) return 0;
long UB = pArr.GetUpperBound(0);
long MaxVal = long.MinValue;
for (int I = 0; I <= UB; I++) {
if (1 <= I && I <= UB - 1) {
if (pArr[I] == I + 1) {
if (MaxVal == I) {
return 1;
}
}
}
MaxVal = Math.Max(MaxVal, pArr[I]);
}
if (pArr[0] == 1) return 1;
if (pArr[UB] == MaxVal) return 1;
long[] CloneArr = (long[])pArr.Clone();
Array.Sort(CloneArr, 0, pArr.Length - 1);
Array.Sort(CloneArr, 1, pArr.Length - 1);
long InvCnt2 = DeriveInvCnt(CloneArr);
if (InvCnt2 == 0) return 2;
Array.Sort(pArr, 1, pArr.Length - 1);
Array.Sort(pArr, 0, pArr.Length - 1);
long InvCnt3 = DeriveInvCnt(pArr);
if (InvCnt3 == 0) return 2;
return 3;
}
////////////////////////////////////////////////////////////////
// 列挙を引数として、列挙内での転倒数を返す
////////////////////////////////////////////////////////////////
static long DeriveInvCnt(IEnumerable<long> pEnum)
{
long[] pArr = pEnum.ToArray();
Dictionary<long, long> ZaatuDict = DeriveZaatuDict(pArr);
long TentouCnt = 0;
var Ins_Fenwick_Tree = new Fenwick_Tree(ZaatuDict.Count);
long UB = ZaatuDict.Count - 1;
foreach (long EachVal in pArr) {
long ZaatuVal = ZaatuDict[EachVal];
TentouCnt += Ins_Fenwick_Tree.GetSum(ZaatuVal + 1, UB, true);
Ins_Fenwick_Tree.Add(ZaatuVal, 1, true);
}
return TentouCnt;
}
//////////////////////////////////////////////////////////////////////////
// 列挙を引数として、座標圧縮し、座圧後の値[座圧前の値]なDictを返す
//////////////////////////////////////////////////////////////////////////
static Dictionary<long, long> DeriveZaatuDict(IEnumerable<long> pEnum)
{
var ZaatuDict = new Dictionary<long, long>();
var ValSet = new HashSet<long>(pEnum);
long No = 0;
foreach (long EachVal in ValSet.OrderBy(pX => pX)) {
ZaatuDict[EachVal] = No;
No++;
}
return ZaatuDict;
}
}
#region Fenwick_Tree
// フェニック木
internal class Fenwick_Tree
{
private long[] mBitArr;
private long mN;
// コンストラクタ
internal Fenwick_Tree(long pItemCnt)
{
mN = pItemCnt;
mBitArr = new long[pItemCnt + 1];
}
// [pSta,pEnd] のSumを返す
internal long GetSum(long pSta, long pEnd, bool pIsZeroOrigin)
{
return GetSum(pEnd, pIsZeroOrigin) - GetSum(pSta - 1, pIsZeroOrigin);
}
// [0,pEnd] のSumを返す
internal long GetSum(long pEnd, bool pIsZeroOrigin)
{
if (pIsZeroOrigin) {
pEnd++; // 1オリジンに変更
}
long Sum = 0;
while (pEnd >= 1) {
Sum += mBitArr[pEnd];
pEnd -= pEnd & -pEnd;
}
return Sum;
}
// [I] に Xを加算
internal void Add(long pI, long pX, bool pIsZeroOrigin)
{
if (pIsZeroOrigin) {
pI++; // 1オリジンに変更
}
while (pI <= mN) {
mBitArr[pI] += pX;
pI += pI & -pI;
}
}
}
#endregion