import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
input=sys.stdin.readline
#sys.setrecursionlimit(10**7)
mo=10**9+7
def cin():
    return map(int,sin().split())
def ain():
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
def pref(a,n,f):
    pre=[0]*n
    if(f==0):         ##from beginning
        pre[0]=a[0]
        for i in range(1,n):
            pre[i]=a[i]+pre[i-1]
    else:              ##from end
        pre[-1]=a[-1]
        for i in range(n-2,-1,-1):
            pre[i]=pre[i+1]+a[i]
    return pre
maxint=10**24
def kadane(a,size):
    max_so_far = -maxint - 1
    max_ending_here = 0

    for i in range(0, size):
        max_ending_here = max_ending_here + a[i]
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here

        if max_ending_here < 0:
            max_ending_here = 0
    return max_so_far
##def maxPrimeFactors (n):
##    maxPrime = -1
##    while n % 2 == 0:
##        maxPrime = 2
##        n >>= 1     # equivalent to n /= 2
##    for i in range(3, int(math.sqrt(n)) + 1, 2):
##        while n % i == 0:
##            maxPrime = i
##            n = n / i
##    if n > 2:
##        maxPrime = n
##    return int(maxPrime)
##dp=[0]*64
##dp[0]=1
##for i in range(1,64):
##    dp[i]=dp[i-1]*2
for _ in range(inin()):
    n=inin()
    l=ain()
    a=sorted(l)
    c=0
    grp=f=0
    cg=[0]*n
    for i in range(n):
        if(a[i]==l[i]):
            cg[i]=1
            c+=1
    if(c==n):
        ans=0
    elif(c==0):
        ans=1
    else:
        fi=cg.index(1)
        li=n-cg[::-1].index(1)-1
        if(c==li+1 or (li==n-1 and li-fi+1==c)):
            ans=1
        else:
            ans=2
    print(ans)

##    maxa=[0]*n
##    maxa[0]=l[0]
##    for i in range(1,n):
##        maxa[i]=max(l[i],maxa[i-1])
##    d=dd(int)
##    co=dd(int)
##    for i in range(n):
##        d[l[i]]=i+1
##        co[l[i]]+=1
##    mi=min(l)
##    cmi=l.count(mi)
##    ma=max(l)
##    so=sorted(l)
##    winpo=ma-so.index(ma)
##    for i in range(winpo,ma):
##        p=b.bisect_right(


import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
input=sys.stdin.readline
#sys.setrecursionlimit(10**7)
mo=10**9+7
def cin():
    return map(int,sin().split())
def ain():
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
def pref(a,n,f):
    pre=[0]*n
    if(f==0):         ##from beginning
        pre[0]=a[0]
        for i in range(1,n):
            pre[i]=a[i]+pre[i-1]
    else:              ##from end
        pre[-1]=a[-1]
        for i in range(n-2,-1,-1):
            pre[i]=pre[i+1]+a[i]
    return pre
maxint=10**24
def kadane(a,size):
    max_so_far = -maxint - 1
    max_ending_here = 0

    for i in range(0, size):
        max_ending_here = max_ending_here + a[i]
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here

        if max_ending_here < 0:
            max_ending_here = 0
    return max_so_far
##def maxPrimeFactors (n):
##    maxPrime = -1
##    while n % 2 == 0:
##        maxPrime = 2
##        n >>= 1     # equivalent to n /= 2
##    for i in range(3, int(math.sqrt(n)) + 1, 2):
##        while n % i == 0:
##            maxPrime = i
##            n = n / i
##    if n > 2:
##        maxPrime = n
##    return int(maxPrime)
##dp=[0]*64
##dp[0]=1
##for i in range(1,64):
##    dp[i]=dp[i-1]*2
for _ in range(inin()):
    n=inin()
    l=ain()
    a=sorted(l)
    c=0
    grp=f=0
    cg=[0]*n
    for i in range(n):
        if(a[i]==l[i]):
            cg[i]=1
            c+=1
    if(c==n):
        ans=0
    elif(c==0):
        ans=1
    else:
        fi=cg.index(0)
        li=n-cg[::-1].index(0)-1
        c=n-c
        if(li-fi+1==c):
            ans=1
        else:
            ans=2
    print(ans)

##    maxa=[0]*n
##    maxa[0]=l[0]
##    for i in range(1,n):
##        maxa[i]=max(l[i],maxa[i-1])
##    d=dd(int)
##    co=dd(int)
##    for i in range(n):
##        d[l[i]]=i+1
##        co[l[i]]+=1
##    mi=min(l)
##    cmi=l.count(mi)
##    ma=max(l)
##    so=sorted(l)
##    winpo=ma-so.index(ma)
##    for i in range(winpo,ma):
##        p=b.bisect_right(


for _ in range(0, int(input())) :
	j = int(input())
	l = list(map(int, input().strip().split()))

	r = 0

	for i in range(1, j + 1):
		if l[i-1] == i :
			r += 1
	if r == j:
		print(0)
	elif r == 0:
		print(1)
	else:
		print(2)


for _ in range(0, int(input())) :
	j = int(input())
	l = list(map(int, input().strip().split()))

	r = 0

	index = []
	flag = 0
	length = 0

	for i in range(1, j + 1):
		if l[i-1] == i :
			r += 1
			index.append(       i     )
			# print(index)
			if r > 1:
				if index[-1] - 1 != index[-2]:
					flag = 1

	# print(index)

	if r == j:
		print(0)
	elif r == 0:
		print(1)
	elif r == 1:
		if index[0] in [1, j]:
			print(1)
		else:
			print(2)
	elif flag == 0 and r!= 1 and ((index[0] in [1,j] ) or (index[-1] in [1,j ])  ):
		print(1)
	elif flag == 0 and (index[-1] - index[0 ] = len(index) - 1):
		print(1)
	else:
		print(2)


for _ in range(0, int(input())) :
	j = int(input())
	l = list(map(int, input().strip().split()))

	r = 0

	index = []
	flag = 0
	length = 0

	for i in range(1, j + 1):
		if l[i-1] == i :
			r += 1
			index.append(       i     )
			# print(index)
			if r > 1:
				if index[-1] - 1 != index[-2]:
					flag = 1

	# print(index)

	if r == j:
		print(0)
	elif r == 0:
		print(1)
	elif r == 1:
		if index[0] in [1, j]:
			print(1)
		else:
			print(2)
	elif flag == 0 and r!= 1 and ((index[0] in [1,j] ) or (index[-1] in [1,j ])  ):
		print(1)
	elif index[0] == 1 and index[-1] == j:
		flag9 = 0
		counter = 0
		while True:
			if flag9 == 1:
				if counter == j - 1:
					print(1)
					break
				else:
					print(2)
					break
			if index[counter] == counter + 1:
				counter += 1
			else:
				flag9 += 1
				counter = index[counter] - 1

	else:
		print(2)


for _ in range(0, int(input())) :
	j = int(input())
	l = list(map(int, input().strip().split()))

	r = 0

	index = []
	flag = 0
	length = 0

	for i in range(1, j + 1):
		if l[i-1] == i :
			r += 1
			index.append(       i     )
			# print(index)
			if r > 1:
				if index[-1] - 1 != index[-2]:
					flag = 1

	# print(index)

	if r == j:
		print(0)
	elif r == 0:
		print(1)
	elif r == 1:
		if index[0] in [1, j]:
			print(1)
		else:
			print(2)
	elif flag == 0 and r!= 1 and ((index[0] in [1,j] ) or (index[-1] in [1,j ])  ):
		print(1)
	else:
		print(2)


for _ in range(0, int(input())) :
	j = int(input())
	l = list(map(int, input().strip().split()))

	r = 0

	index = []
	flag = 0
	length = 0

	for i in range(1, j + 1):
		if l[i-1] == i :
			r += 1
			index.append(       i     )
			# print(index)
			if r > 1:
				if index[-1] - 1 != index[-2]:
					flag = 1

	# print(index)

	if r == j:
		print(0)
	elif r == 0:
		print(1)
	elif r == 1:
		if index[0] in [1, j]:
			print(1)
		else:
			print(2)
	elif flag == 0 and r!= 1:
		print(1)
	else:
		print(2)


for _ in range(0, int(input())) :
	j = int(input())
	l = list(map(int, input().strip().split()))

	r = 0

	index = []
	flag = 0
	length = 0

	for i in range(1, j + 1):
		if l[i-1] == i :
			r += 1
			index.append(       i     )
			# print(index)
			if r > 1:
				if index[-1] - 1 != index[-2]:
					flag = 1

	# print(index)

	if r == j:
		print(0)
	elif r == 0:
		print(1)
	elif r == 1:
		if index[0] in [1, j]:
			print(1)
		else:
			print(2)
	elif flag == 0 and r!= 1 and ((index[0] in [1,j] ) or (index[-1] in [1,j ])  ):
		print(1)
	elif flag == 0 and r!= 1 and (index[-1] - index[0 ] == len(index) - 1):
		print(1)
	else:
		print(2)


for _ in range(0, int(input())) :
	j = int(input())
	l = list(map(int, input().strip().split()))

	r = 0

	index = []
	flag = 0
	length = 0

	for i in range(1, j + 1):
		if l[i-1] == i :
			r += 1
			index.append(       i     )
			# print(index)
			if r > 1:
				if index[-1] - 1 != index[-2]:
					flag = 1

	if r == j:
		print(0)
	elif r == 0:
		print(1)
	elif flag == 0:
		print(1)
	else:
		print(2)


for _ in range(0, int(input())) :
	j = int(input())
	l = list(map(int, input().strip().split()))

	r = 0

	index = []
	flag = 0
	length = 0

	for i in range(1, j + 1):
		if l[i-1] == i :
			r += 1
			index.append(       i     )
			# print(index)
			if r > 1:
				if index[-1] - 1 != index[-2]:
					flag = 1

	# print(index)

	if r == j:
		print(0)
	elif r == 0:
		print(1)
	elif r == 1:
		if index[0] in [1, j]:
			print(1)
		else:
			print(2)
	elif flag == 0 and r!= 1 and ((index[0] in [1,j] ) or (index[-1] in [1,j ])  ):
		print(1)
	elif flag == 0 and (index[-1] - index[0 ] == len(index) - 1):
		print(1)
	else:
		print(2)


t = int(input())

for i in range(t):

    n = int(input())
    arr = list(map(int, input().split()))
    chcounter = 0

    if arr[0] == 1:
        falseflag = False
        trueflag = True
    else:
        falseflag = True
        trueflag = False

    for j in range(n):
        if arr[j] == (j + 1):
            if falseflag:
                falseflag = False
                trueflag = True
                chcounter += 1
                if chcounter > 1:
                    break

        else:
            if trueflag:
                trueflag = False
                falseflag = True
                chcounter += 1
                if chcounter > 1:
                    break

    if chcounter == 0:
        print(0)
    elif chcounter == 1:
        print(1)
    elif chcounter > 1:
        print(2)


t = int(input())

for i in range(t):

    n = int(input())
    arr = list(map(int, input().split()))
    chcounter = 0

    if arr[0] == 1:
        falseflag = False
        trueflag = True
    else:
        falseflag = True
        trueflag = False

    for j in range(n):
        if arr[j] == (j + 1):
            if falseflag:
                falseflag = False
                trueflag = True
                chcounter += 1
                if chcounter > 1:
                    break

        else:
            if trueflag:
                trueflag = False
                falseflag = True
                chcounter += 1
                if chcounter > 1:
                    break

    if chcounter == 0:
        if trueflag == True:
            print(0)
        else:
            print(1)
    elif chcounter == 1:
        print(1)
    elif chcounter > 1:
        print(2)


t = int(input())

for i in range(t):

    n = int(input())
    arr = list(map(int, input().split()))
    chcounter = 0

    if arr[0] == 1:
        falseflag = False
        trueflag = True
    else:
        falseflag = True
        trueflag = False

    for j in range(n):
        if arr[j] == (j + 1):
            if falseflag:
                falseflag = False
                trueflag = True
                chcounter += 1
                if chcounter > 2:
                    break

        else:
            if trueflag:
                trueflag = False
                falseflag = True
                chcounter += 1
                if chcounter > 2:
                    break

    if chcounter == 0:
        if trueflag == True:
            print(0)
        else:
            print(1)
    elif chcounter == 1:
        print(1)
    elif chcounter == 2:
        if arr[0] == 1 and arr[-1] == n:
            print(1)
        else:
            print(2)
    elif chcounter > 2:
            print(2)


from collections import Counter
import string
import math
import sys
from fractions import Fraction
def array_int():
    return [int(i) for i in sys.stdin.readline().split()]
def vary(arrber_of_variables):
    if arrber_of_variables==1:
        return int(sys.stdin.readline())
    if arrber_of_variables>=2:
        return map(int,sys.stdin.readline().split())
def makedict(var):
    return dict(Counter(var))
# i am noob wanted to be better and trying hard for that
def printDivisors(n):
    divisors=[]
    # Note that this loop runs till square root
    i = 1
    while i <= math.sqrt(n):

        if (n % i == 0) :

            # If divisors are equal, print only one
            if (n//i == i) :
                divisors.append(i)
            else :
                # Otherwise print both
                divisors.extend((i,n//i))
        i = i + 1
    return divisors

def countTotalBits(num):

     # convert number into it's binary and
     # remove first two characters 0b.
     binary = bin(num)[2:]
     return(len(binary))
def isPrime(n) :

    # Corner cases
    if (n <= 1) :
        return False
    if (n <= 3) :
        return True

    # This is checked so that we can skip
    # middle five numbers in below loop
    if (n % 2 == 0 or n % 3 == 0) :
        return False

    i = 5
    while(i * i <= n) :
        if (n % i == 0 or n % (i + 2) == 0) :
            return False
        i = i + 6

    return True
""" def dfs(node,val):
    global tree,visited
    visited[node]=1
    ans[node]=val
    val^=1
    for i in tree[node]:
        if visited[i]==-1:
            dfs(i,val) """
for _ in range(vary(1)):
    n=vary(1)
    num=array_int()
    count=0
    for i in range(1,n+1):
        while num[i-1]!=i:
            count+=1
            # print(num)
            t=num[i-1]
            num[i-1]=num[num[i-1]-1]
            num[t-1]=t
            # print(num)
            break
    print(count)


from collections import Counter
import string
import math
import sys
from fractions import Fraction
def array_int():
    return [int(i) for i in sys.stdin.readline().split()]
def vary(arrber_of_variables):
    if arrber_of_variables==1:
        return int(sys.stdin.readline())
    if arrber_of_variables>=2:
        return map(int,sys.stdin.readline().split())
def makedict(var):
    return dict(Counter(var))
# i am noob wanted to be better and trying hard for that
def printDivisors(n):
    divisors=[]
    # Note that this loop runs till square root
    i = 1
    while i <= math.sqrt(n):

        if (n % i == 0) :

            # If divisors are equal, print only one
            if (n//i == i) :
                divisors.append(i)
            else :
                # Otherwise print both
                divisors.extend((i,n//i))
        i = i + 1
    return divisors

def countTotalBits(num):

     # convert number into it's binary and
     # remove first two characters 0b.
     binary = bin(num)[2:]
     return(len(binary))
def isPrime(n) :

    # Corner cases
    if (n <= 1) :
        return False
    if (n <= 3) :
        return True

    # This is checked so that we can skip
    # middle five numbers in below loop
    if (n % 2 == 0 or n % 3 == 0) :
        return False

    i = 5
    while(i * i <= n) :
        if (n % i == 0 or n % (i + 2) == 0) :
            return False
        i = i + 6

    return True
""" mod=10**9+7
def ncr(n,r):
    if n<r:
        return 0
    numer=fact[n]
    # print(numer)
    denm=(fact[n-r]*fact[r])
    # print(denm)
    return numer*pow(denm,mod-2,mod) """
""" def dfs(node,dis):
    global graph,visited,special
    if node in special:
        return dis
    else:
        dis+=1

    visited[node]=1

    for i in graph[node]:
        if visited[i]==-1:
            dfs(i,dis)
    return -1 """
""" fact=[1]*(1001)
c=1
mod=10**9+7
for i in range(1,1001):
    fact[i]=(fact[i-1]*i)%mod """
# print(fact)
for _ in range(vary(1)):
    n=vary(1)
    num=array_int()
    mark=[0 for i in range(n)]
    for i in range(n):
        if num[i]!=i+1:
            mark[i]=1
    count=0
    flag=1
    # print(mark)
    for i in range(n):
        if mark[i]==1:
            if i==n-1:
                count+=1
            flag=0
            continue
        elif flag!=1:
            flag=1
            count+=1
    print(count)


from collections import Counter
import string
import math
import sys
from fractions import Fraction
def array_int():
    return [int(i) for i in sys.stdin.readline().split()]
def vary(arrber_of_variables):
    if arrber_of_variables==1:
        return int(sys.stdin.readline())
    if arrber_of_variables>=2:
        return map(int,sys.stdin.readline().split())
def makedict(var):
    return dict(Counter(var))
# i am noob wanted to be better and trying hard for that
def printDivisors(n):
    divisors=[]
    # Note that this loop runs till square root
    i = 1
    while i <= math.sqrt(n):

        if (n % i == 0) :

            # If divisors are equal, print only one
            if (n//i == i) :
                divisors.append(i)
            else :
                # Otherwise print both
                divisors.extend((i,n//i))
        i = i + 1
    return divisors

def countTotalBits(num):

     # convert number into it's binary and
     # remove first two characters 0b.
     binary = bin(num)[2:]
     return(len(binary))
def isPrime(n) :

    # Corner cases
    if (n <= 1) :
        return False
    if (n <= 3) :
        return True

    # This is checked so that we can skip
    # middle five numbers in below loop
    if (n % 2 == 0 or n % 3 == 0) :
        return False

    i = 5
    while(i * i <= n) :
        if (n % i == 0 or n % (i + 2) == 0) :
            return False
        i = i + 6

    return True
""" mod=10**9+7
def ncr(n,r):
    if n<r:
        return 0
    numer=fact[n]
    # print(numer)
    denm=(fact[n-r]*fact[r])
    # print(denm)
    return numer*pow(denm,mod-2,mod) """
""" def dfs(node,dis):
    global graph,visited,special
    if node in special:
        return dis
    else:
        dis+=1

    visited[node]=1

    for i in graph[node]:
        if visited[i]==-1:
            dfs(i,dis)
    return -1 """
""" fact=[1]*(1001)
c=1
mod=10**9+7
for i in range(1,1001):
    fact[i]=(fact[i-1]*i)%mod """
# print(fact)
for _ in range(vary(1)):
    n=vary(1)
    num=array_int()
    mark=[0 for i in range(n)]
    for i in range(n):
        if num[i]!=i+1:
            mark[i]=1
    count=0
    flag=1
    # print(mark)
    for i in range(n):
        if mark[i]==1:
            flag=0
            continue
        elif flag!=1:
            flag=1
            count+=1
    print(count)


from collections import Counter
import string
import math
import sys
from fractions import Fraction
def array_int():
    return [int(i) for i in sys.stdin.readline().split()]
def vary(arrber_of_variables):
    if arrber_of_variables==1:
        return int(sys.stdin.readline())
    if arrber_of_variables>=2:
        return map(int,sys.stdin.readline().split())
def makedict(var):
    return dict(Counter(var))
# i am noob wanted to be better and trying hard for that
def printDivisors(n):
    divisors=[]
    # Note that this loop runs till square root
    i = 1
    while i <= math.sqrt(n):

        if (n % i == 0) :

            # If divisors are equal, print only one
            if (n//i == i) :
                divisors.append(i)
            else :
                # Otherwise print both
                divisors.extend((i,n//i))
        i = i + 1
    return divisors

def countTotalBits(num):

     # convert number into it's binary and
     # remove first two characters 0b.
     binary = bin(num)[2:]
     return(len(binary))
def isPrime(n) :

    # Corner cases
    if (n <= 1) :
        return False
    if (n <= 3) :
        return True

    # This is checked so that we can skip
    # middle five numbers in below loop
    if (n % 2 == 0 or n % 3 == 0) :
        return False

    i = 5
    while(i * i <= n) :
        if (n % i == 0 or n % (i + 2) == 0) :
            return False
        i = i + 6

    return True
""" mod=10**9+7
def ncr(n,r):
    if n<r:
        return 0
    numer=fact[n]
    # print(numer)
    denm=(fact[n-r]*fact[r])
    # print(denm)
    return numer*pow(denm,mod-2,mod) """
""" def dfs(node,dis):
    global graph,visited,special
    if node in special:
        return dis
    else:
        dis+=1

    visited[node]=1

    for i in graph[node]:
        if visited[i]==-1:
            dfs(i,dis)
    return -1 """
""" fact=[1]*(1001)
c=1
mod=10**9+7
for i in range(1,1001):
    fact[i]=(fact[i-1]*i)%mod """
# print(fact)
for _ in range(vary(1)):
    n=vary(1)
    num=array_int()
    mark=[0 for i in range(n)]
    for i in range(n):
        if num[i]!=i+1:
            mark[i]=1
    count=0
    flag=1
    # print(mark)
    for i in range(n):
        if mark[i]==1:
            if i==n-1:
                count+=1
            flag=0
            continue
        elif flag!=1:
            flag=1
            count+=1
    if count>2:
        print(2)
    else:
        print(count)


import os,io
from sys import stdout
import collections
# import random
# import math
# from operator import itemgetter
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# from collections import Counter
# from decimal import Decimal
# import heapq
# from functools import lru_cache

import sys
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(102400000)

# from functools import lru_cache
# @lru_cache(maxsize=None)

######################
# --- Maths Fns  --- #
######################
def primes(n):
    sieve = [True] * n
    for i in range(3,int(n**0.5)+1,2):
        if sieve[i]:
            sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1)
    return [2] + [i for i in range(3,n,2) if sieve[i]]

def binomial_coefficient(n, k):
    if 0 <= k <= n:
        ntok = 1
        ktok = 1
        for t in range(1, min(k, n - k) + 1):
            ntok *= n
            ktok *= t
            n -= 1
        return ntok // ktok
    else:
        return 0

def powerOfK(k, max):
    if k == 1:
        return [1]
    if k == -1:
        return [-1, 1]

    result = []
    n = 1
    while n <= max:
        result.append(n)
        n *= k
    return result

def divisors(n):
    i = 1
    result = []
    while i*i <= n:
        if n%i == 0:
            if n/i == i:
                result.append(i)
            else:
                result.append(i)
                result.append(n/i)
        i+=1
    return result

def distance(xa, ya, xb, yb):
    return ((ya-yb)**2 + (xa-xb)**2)**(0.5)

# @lru_cache(maxsize=None)
def digitsSum(n):
    if n == 0:
        return 0
    r = 0
    while n > 0:
        r += n % 10
        n //= 10
    return r

######################
# ---- GRID Fns ---- #
######################
def isValid(i, j, n, m):
    return i >= 0 and i < n and j >= 0 and j < m

def print_grid(grid):
    for line in grid:
        print(" ".join(map(str,line)))

######################
# ---- MISC Fns ---- #
######################
def kadane(a,size):
    max_so_far = 0
    max_ending_here = 0

    for i in range(0, size):
        max_ending_here = max_ending_here + a[i]
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here

        if max_ending_here < 0:
            max_ending_here = 0
    return max_so_far

def prefixSum(arr):
    for i in range(1, len(arr)):
        arr[i] = arr[i] + arr[i-1]
    return arr

def ceil(n, d):
    if n % d == 0:
        return n // d
    else:
        return (n // d) + 1

def _abs(a, b):
    return int(abs(a - b))

def evenodd(l):
    even = [e for e in l if e & 1 == 0]
    odd = [e for e in l if e & 1]
    return (even, odd)

# INPUTS  --------------------------
# s = input().decode('utf-8').strip()
# n = int(input())
# l = list(map(int, input().split()))
# t = int(input())
# for _ in range(t):

t = int(input())
for _ in range(t):
    n = int(input())
    l = list(map(int, input().split()))

    correct = []
    incorrect = []
    for i, v in enumerate(l):
        if i+1 == v:
            correct.append((i, v))
        else:
            incorrect.append((i,v))

    c = len(correct)

    if c == n:
        print(0)
    elif c == 0:
        print(1)
    elif c == 1:
        print(2)
    else:
        ic = sorted(incorrect, key=lambda x: x[0])
        done = False
        for i in range(1, len(ic)):
            if ic[i][0] != ic[i-1][0]+1:
                print(2)
                done = True
                break
        if not done:
            print(1)


import os,io
from sys import stdout
import collections
# import random
# import math
# from operator import itemgetter
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# from collections import Counter
# from decimal import Decimal
# import heapq
# from functools import lru_cache

import sys
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(102400000)

# from functools import lru_cache
# @lru_cache(maxsize=None)

######################
# --- Maths Fns  --- #
######################
def primes(n):
    sieve = [True] * n
    for i in range(3,int(n**0.5)+1,2):
        if sieve[i]:
            sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1)
    return [2] + [i for i in range(3,n,2) if sieve[i]]

def binomial_coefficient(n, k):
    if 0 <= k <= n:
        ntok = 1
        ktok = 1
        for t in range(1, min(k, n - k) + 1):
            ntok *= n
            ktok *= t
            n -= 1
        return ntok // ktok
    else:
        return 0

def powerOfK(k, max):
    if k == 1:
        return [1]
    if k == -1:
        return [-1, 1]

    result = []
    n = 1
    while n <= max:
        result.append(n)
        n *= k
    return result

def divisors(n):
    i = 1
    result = []
    while i*i <= n:
        if n%i == 0:
            if n/i == i:
                result.append(i)
            else:
                result.append(i)
                result.append(n/i)
        i+=1
    return result

def distance(xa, ya, xb, yb):
    return ((ya-yb)**2 + (xa-xb)**2)**(0.5)

# @lru_cache(maxsize=None)
def digitsSum(n):
    if n == 0:
        return 0
    r = 0
    while n > 0:
        r += n % 10
        n //= 10
    return r

######################
# ---- GRID Fns ---- #
######################
def isValid(i, j, n, m):
    return i >= 0 and i < n and j >= 0 and j < m

def print_grid(grid):
    for line in grid:
        print(" ".join(map(str,line)))

######################
# ---- MISC Fns ---- #
######################
def kadane(a,size):
    max_so_far = 0
    max_ending_here = 0

    for i in range(0, size):
        max_ending_here = max_ending_here + a[i]
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here

        if max_ending_here < 0:
            max_ending_here = 0
    return max_so_far

def prefixSum(arr):
    for i in range(1, len(arr)):
        arr[i] = arr[i] + arr[i-1]
    return arr

def ceil(n, d):
    if n % d == 0:
        return n // d
    else:
        return (n // d) + 1

def _abs(a, b):
    return int(abs(a - b))

def evenodd(l):
    even = [e for e in l if e & 1 == 0]
    odd = [e for e in l if e & 1]
    return (even, odd)

# INPUTS  --------------------------
# s = input().decode('utf-8').strip()
# n = int(input())
# l = list(map(int, input().split()))
# t = int(input())
# for _ in range(t):
def largest_prime_factor(n):
    i = 2
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
    return n

t = int(input())
for _ in range(t):
    n = int(input())
    l = list(map(int, input().split()))

    i = 1
    correct = []
    for e in l:
        if e == i:
            correct.append(e)
        i+=1

    if len(correct) == n:
        print(0)
    elif len(correct) == 0:
        print(1)
    elif len(correct) == 1:
        if correct[0] == 1 or correct[0] == n:
            print(1)
        else:
            print(2)
    else:
        correct = sorted(correct)
        possible = True
        for i in range(1, len(correct)):
            if correct[i] != correct[i-1]+1:
                possible = False
                break

        if possible and (correct[0] == 1 or correct[-1] == n):
            print(1)
        else:
            print(2)


import os,io
from sys import stdout
import collections
# import random
# import math
# from operator import itemgetter
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# from collections import Counter
# from decimal import Decimal
# import heapq
# from functools import lru_cache

import sys
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(102400000)

# from functools import lru_cache
# @lru_cache(maxsize=None)

######################
# --- Maths Fns  --- #
######################
def primes(n):
    sieve = [True] * n
    for i in range(3,int(n**0.5)+1,2):
        if sieve[i]:
            sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1)
    return [2] + [i for i in range(3,n,2) if sieve[i]]

def binomial_coefficient(n, k):
    if 0 <= k <= n:
        ntok = 1
        ktok = 1
        for t in range(1, min(k, n - k) + 1):
            ntok *= n
            ktok *= t
            n -= 1
        return ntok // ktok
    else:
        return 0

def powerOfK(k, max):
    if k == 1:
        return [1]
    if k == -1:
        return [-1, 1]

    result = []
    n = 1
    while n <= max:
        result.append(n)
        n *= k
    return result

def divisors(n):
    i = 1
    result = []
    while i*i <= n:
        if n%i == 0:
            if n/i == i:
                result.append(i)
            else:
                result.append(i)
                result.append(n/i)
        i+=1
    return result

def distance(xa, ya, xb, yb):
    return ((ya-yb)**2 + (xa-xb)**2)**(0.5)

# @lru_cache(maxsize=None)
def digitsSum(n):
    if n == 0:
        return 0
    r = 0
    while n > 0:
        r += n % 10
        n //= 10
    return r

######################
# ---- GRID Fns ---- #
######################
def isValid(i, j, n, m):
    return i >= 0 and i < n and j >= 0 and j < m

def print_grid(grid):
    for line in grid:
        print(" ".join(map(str,line)))

######################
# ---- MISC Fns ---- #
######################
def kadane(a,size):
    max_so_far = 0
    max_ending_here = 0

    for i in range(0, size):
        max_ending_here = max_ending_here + a[i]
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here

        if max_ending_here < 0:
            max_ending_here = 0
    return max_so_far

def prefixSum(arr):
    for i in range(1, len(arr)):
        arr[i] = arr[i] + arr[i-1]
    return arr

def ceil(n, d):
    if n % d == 0:
        return n // d
    else:
        return (n // d) + 1

def _abs(a, b):
    return int(abs(a - b))

def evenodd(l):
    even = [e for e in l if e & 1 == 0]
    odd = [e for e in l if e & 1]
    return (even, odd)

# INPUTS  --------------------------
# s = input().decode('utf-8').strip()
# n = int(input())
# l = list(map(int, input().split()))
# t = int(input())
# for _ in range(t):

t = int(input())
for _ in range(t):
    n = int(input())
    l = list(map(int, input().split()))

    correct = []
    incorrect = []
    for i, v in enumerate(l):
        if i+1 == v:
            correct.append((i, v))
        else:
            incorrect.append((i,v))

    c = len(correct)

    if c == n:
        print(0)
    elif c == 0:
        print(1)
    else:
        ic = sorted(incorrect, key=lambda x: x[0])
        done = False
        for i in range(1, len(ic)):
            if ic[i][0] != ic[i-1][0]+1:
                print(2)
                done = True
                break
        if not done:
            print(1)


for nt in range(int(input())):
	n = int(input())
	a = list(map(int,input().split()))
	b = sorted(a)
	if a==b:
		print (0)
		continue
	else:
		flag = []
		for i in range(n):
			if a[i]==i+1:
				flag.append(i)
		if len(flag)!=0:
			if flag[0]==0 and flag[-1]==len(flag)-1:
				print (1)
			elif flag[-1]==n-1 and flag[0]==n-len(flag):
				print (1)
			else:
				print (2)
		else:
			print (1)


for nt in range(int(input())):
	n = int(input())
	a = list(map(int,input().split()))
	b = sorted(a)
	if a==b:
		print (0)
		continue
	else:
		flag = 0
		for i in range(n):
			if a[i]==i+1:
				flag = 1
				break
		if flag:
			if len(a)==3 and a[1]==2:
				print (3)
			else:
				print (2)
		else:
			print (1)


for nt in range(int(input())):
	n = int(input())
	a = list(map(int,input().split()))
	b = sorted(a)
	if a==b:
		print (0)
		continue
	else:
		flag = 0
		for i in range(n):
			if a[i]==i+1:
				flag = 1
				break
		if flag:
			if len(a)==3:
				print (3)
			else:
				print (2)
		else:
			print (1)


for nt in range(int(input())):
	n = int(input())
	a = list(map(int,input().split()))
	b = sorted(a)
	if a==b:
		print (0)
		continue
	else:
		flag = []
		for i in range(n):
			if a[i]!=i+1:
				flag.append(i)
		# print (flag)
		if len(flag)!=0:
			if flag[-1] - flag[0] == len(flag)-1:
				print (1)
			else:
				print (2)
		else:
			print (1)


for _ in range(int(input())):
    n = int(input())
    a = list(map(int,input().split()))
    b = sorted(a)
    if a==b:
        print("0")
        continue
    cnt=0
    i=0
    while a[i]==b[i]:
        i+=1
        if i==n:
            break
    ind1=i
    i=n-1
    while a[i]==b[i]:
        i-=1
        if i==-1:
            break
    ind2=i
    for i in range(ind1,ind2+1):
        if a[i]==b[i]:
            cnt+=1
    if cnt==0:
        print("1")
    else:
        print("2")


for _ in range(int(input())):
    n = int(input())
    a = list(map(int,input().split()))
    b = sorted(a)
    cnt=0
    i=0
    while a[i]==b[i]:
        i+=1
        if i==n:
            break
    ind1=i
    i=n-1
    while a[i]==b[i]:
        i-=1
        if i==-1:
            break
    ind2=i
    for i in range(ind1,ind2+1):
        if a[i]!=b[i]:
            cnt+=1
    if cnt==0:
        print("0")
    else:
        print("2")


for _ in range(int(input())):
    n = int(input())
    a = list(map(int,input().split()))
    b = sorted(a)
    if a==b:
        print("0")
        continue
    cnt=0
    i=0
    while a[i]==b[i]:
        i+=1
        if i==n:
            break
    ind1=i
    i=n-1
    while a[i]==b[i]:
        i-=1
        if i==-1:
            break
    ind2=i
    for i in range(ind1,ind2+1):
        if a[i]!=b[i]:
            cnt+=1
    if cnt==0:
        print("1")
    else:
        print("2")


def cin():
    return map(int, input().split())

# for t in range(int(input())):
#     n, x = cin()
#     a = list(cin())
#     b = sorted(a, reverse=True)
#     ans = 0
#     j = 0
#     for i in range(n):
#         if b[i] * (i - j + 1) >= x:
#             ans += 1
#             j = i + 1
#     print(ans)

# for t in range(int(input())):
#     s = input()
#     n = len(s)
#     pref = [0]
#     l = float('inf')
#     for c in s:
#         pref.append(pref[-1] + (1, -1)[c == '-'])
#         l = min(l, pref[-1])
#     init = max(0, l * -1)
#     ans = 0
#     j = 1
#     for i in range(init):
#         while i + pref[j] >= 0:
#             j += 1
#         ans += j
#         j += 1
#     print(ans + n)

for t in range(int(input())):
    n = int(input())
    a = list(cin())
    cnt = 0
    for i in range(n):
        if a[i] == i + 1:
            cnt += 1
    if cnt == n:
        print(0)
    else:
        l, r = 0, 0
        for i in range(n):
            if a[i] != i + 1:
                l = i
                break
        for i in range(n - 1, -1, -1):
            if a[i] != i + 1:
                r = n - i - 1
                break
        cnt = cnt - l - r
        if not cnt:
            print(1)
        else:
            print(2)


def cin():
    return map(int, input().split())

# for t in range(int(input())):
#     n, x = cin()
#     a = list(cin())
#     b = sorted(a, reverse=True)
#     ans = 0
#     j = 0
#     for i in range(n):
#         if b[i] * (i - j + 1) >= x:
#             ans += 1
#             j = i + 1
#     print(ans)

# for t in range(int(input())):
#     s = input()
#     n = len(s)
#     pref = [0]
#     l = float('inf')
#     for c in s:
#         pref.append(pref[-1] + (1, -1)[c == '-'])
#         l = min(l, pref[-1])
#     init = max(0, l * -1)
#     ans = 0
#     j = 1
#     for i in range(init):
#         while i + pref[j] >= 0:
#             j += 1
#         ans += j
#         j += 1
#     print(ans + n)

for t in range(int(input())):
    n = int(input())
    a = list(cin())
    cnt = 0
    for i in range(n):
        if a[i] == i + 1:
            cnt += 1
    if cnt == n:
        print(0)
    else:
        cnt = cnt - (a[0] == 1) - (a[n - 1] == n)
        if not cnt:
            print(1)
        else:
            print(2)


def cin():
    return map(int, input().split())

# for t in range(int(input())):
#     n, x = cin()
#     a = list(cin())
#     b = sorted(a, reverse=True)
#     ans = 0
#     j = 0
#     for i in range(n):
#         if b[i] * (i - j + 1) >= x:
#             ans += 1
#             j = i + 1
#     print(ans)

# for t in range(int(input())):
#     s = input()
#     n = len(s)
#     pref = [0]
#     l = float('inf')
#     for c in s:
#         pref.append(pref[-1] + (1, -1)[c == '-'])
#         l = min(l, pref[-1])
#     init = max(0, l * -1)
#     ans = 0
#     j = 1
#     for i in range(init):
#         while i + pref[j] >= 0:
#             j += 1
#         ans += j
#         j += 1
#     print(ans + n)

for t in range(int(input())):
    n = int(input())
    a = list(cin())
    cnt = 0
    for i in range(n):
        if a[i] == i + 1:
            cnt += 1
    if cnt == n:
        print(0)
    elif not cnt:
        print(1)
    else:
        print(2)


# for t in range(int(input())):
#     n = int(input())
#     if not n % 2:
#         print(f'{n // 2} {n // 2}')
#     else:
#         m = 3
#         while n % m > 0 and m <= n // 2:
#             m += 2
#         if m == n // 2 and n % m >= 1:
#             print(f'1 {n - 1}')
#         else:
#             print(f'{n // m} {n - n // m}')

for t in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    i = 0
    while i < n:
        while i < n and a[i] == i + 1:
            i += 1
        j = i
        while i < n and a[i] != i + 1:
            i += 1
        cnt += 1 if j - i else 0
    print(cnt)


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=sorted(a)
    if a==b:print(0);continue
    ans=0
    if a[0]!=1:ans=1
    for i in range(1,n):
        if a[i]!=i+1 and a[i-1]==i:
            ans+=1
    if ans==1:print(ans)
    else:print(2)


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    ans=0
    for i in range(n):
        if a[i]!=i+1 and a[i-1]==i+1:
            ans+=1
    print(ans)


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=a.sort()
    if a==b:print(0);continue
    ans=0
    if a[0]!=1:ans=1
    for i in range(1,n):
        if a[i]!=i+1 and a[i-1]==i:
            ans+=1
    if ans==1:print(ans)
    else:print(2)


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    ans=0
    if a[0]!=1:ans=1
    for i in range(1,n):
        if a[i]!=i+1 and a[i-1]==i:
            ans+=1
    print(ans)


for _ in range(int(input())):
    n = int(input())
    lis=[int(x) for x in input().split()]
    ok = 0
    notok = 0
    for i in range (1,n+1):
        if(i!=lis[i-1]):
            ok = 1
    if(ok==1):
        for i in range(1,n):
            if(i==lis[i-1]):
                notok+=1
    if(ok == 0):
        print(0)
    elif( notok ==0):
        print(1)
    else:
        print(2)


for _ in range(int(input())):
    n = int(input())
    lis=[int(x) for x in input().split()]
    ok = 0
    notok = 0
    for i in range (1,n+1):
        if(i!=lis[i-1]):
            ok = 1
    if(ok==1):
        for i in range(1,n+1):
            if(i==lis[i-1]):
                notok+=1
    if(ok == 0):
        print(0)
    elif(ok==1 and notok ==0):
        print(1)
    else:
        print(2)


for _ in range(int(input())):
    n = int(input())
    lis=[int(x) for x in input().split()]
    ok = 0
    notok = 0
    for i in range (1,n+1):
        if(i!=lis[i-1]):
            ok = 1
    if(ok==1):
        for i in range(1,n):
            if(i==lis[i-1]):
                notok+=1
    if(ok == 0):
        print(0)
    elif(ok==1 and notok ==0):
        print(1)
    else:
        print(2)


for _ in range(int(input())):
    n = int(input())
    lis=[int(x) for x in input().split()]
    ok = 0
    notok = 0
    for i in range (1,n+1):
        if(i!=lis[i-1]):
            ok = 1
    if(ok==1):
        for i in range(1,n):
            if(i==lis[i-1]):
                notok+=1
    if(ok == 0):
        print(0)
    elif(ok==1 and notok !=0):
        print(2)
    else:
        print(1)


for _ in range(int(input())):
    n=int(input())
    l=[int(x) for x in input().split()]
    f=0
    flag=0
    g=0
    bigflag=0
    for i in range(1,n+1):
        if l[i-1]!=i:
            if flag==0:
                f=1
                g=1
            else:
                bigflag=1
                break
        else:
            if g==1:
                flag=1
    if f==0:
        print(0)
    else:
        if bigflag==0:
            print(1)
        else:
            print(2)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	diff = []
	for i in range(n):
		diff.append((i+1)-a[i])

	l = 0
	while l < n:
		if not diff[l]:
			l += 1
		else:
			break
	r = n-1
	while r > l:
		if not diff[r]:
			r -= 1
		else:
			break

	z_count = 0
	for i in range(l, r+1):
		if not diff[i]:
			z_count += 1

	if l != r:
		if z_count:
			print(2)
		else:
			print(1)
	else:
		print(0)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	diff = []
	for i in range(n):
		diff.append((i+1)-a[i])

	l = 0
	while l < n:
		if not diff[l]:
			l += 1
		else:
			break
	r = n-1
	while r > l:
		if not diff[r]:
			r -= 1
		else:
			break

	z_count = 0
	for i in range(l, r+1):
		if not diff[i]:
			z_count += 1

	if l < r:
		if z_count:
			print(2)
		else:
			print(1)
	else:
		print(0)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	diff = []
	for i in range(n):
		diff.append((i+1)-a[i])

	l = 0
	while l < n:
		if not diff[l]:
			l += 1
		else:
			break
	r = l+1; r0 = l

	while r < n and r0 < n:
		if not diff[r]:
			r0 = r
			r += 1
			while r < n:
				if not diff[r]:
					r += 1
				else:
					break
		else:
			r += 1

	if r0 == l:
		r0 = n-1

	if r0 - l > 2:
		print(2)
	elif r0-l == 2:
		print(1)
	else:
		print(0)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = [int(i) for i in input().split()]
	diff = []
	for i in range(n):
		diff.append((i+1)-a[i])

	l = 0
	while l < n:
		if not diff[l]:
			l += 1
		else:
			break
	r = l+1; r0 = l

	while r < n and r0 < n:
		if not diff[r]:
			r0 = r
			r += 1
			while r < n:
				if not diff[r]:
					r += 1
				else:
					break
		else:
			r += 1

	if r0 == l:
		r0 = n-1

	if r0-l == 2 or (r0 == n-1 and l == 0):
		print(1)
	elif r0 - l > 2:
		print(2)
	else:
		print(0)


for _ in range(int(input())):
    n = int(input())
    l = list(map(int,input().split()))
    o = l.copy()
    o.sort()
    if l == o:
        print(0)
        continue

    i = 0
    e = []
    while(i < len(l)):
        if l[i] == i+1:
            e.append(i+1)

        i = i+1

    if e[0] > 1 and e[-1] < n:
        print(2)
        continue

    f = 0
    p = e.pop(0)
    for i in e:
        if i != p+1:
            f = 1
            break

    if f == 1:
        print(2)
        continue

    else:
        print(1)
        continue


for _ in range(int(input())):
    n = int(input())
    l = list(map(int,input().split()))
    o = l.copy()
    o.sort()
    if l == o:
        print(0)
        continue

    i = 0
    e = []
    while(i < len(l)):
        if l[i] == i+1:
            e.append(i+1)

        i = i+1

    if len(e) == 0:
        print(1)
        continue

    f = 0
    p = e.pop(0)
    for i in e:
        if i != p+1:
            f = 1
            break

    if f == 1:
        print(2)

    else:
        print(1)


for _ in range(int(input())):
    n = int(input())
    l = list(map(int,input().split()))
    o = l.copy()
    o.sort()
    if l == o:
        print(0)
        continue

    i = 0
    e = []
    while(i < len(l)):
        if l[i] == i+1:
            e.append(i+1)

        i = i+1

    if len(e) == 0:
        print(1)
        continue

    if e[0] > 1 and e[-1] < n:
        print(2)
        continue

    f = 0
    p = e.pop(0)
    for i in e:
        if i != p+1:
            f = 1
            break

    if f == 1:
        print(2)

    else:
        print(1)


for _ in range(int(input())):
    n = int(input())
    l = list(map(int,input().split()))
    o = l.copy()
    o.sort()
    if l == o:
        print(0)
        continue

    i = 0
    e = []
    s = set()
    while(i < len(l)):
        if l[i] == i+1:
            e.append(i+1)
            s.add(i+1)

        i = i+1

    if len(e) == 0:
        print(1)
        continue

    if e[0] > 1 and e[-1] < n:
        print(2)
        continue

    f = 0
    ix = 1
    while(ix <= n and f == 0):
        if ix in s:
            s.remove(ix)
            ix = ix+1

        else:
            f = 1

    f = 0
    ix = n
    while (ix >= 1 and f == 0):
        if ix in s:
            s.remove(ix)
            ix = ix -1

        else:
            f = 1

    if len(s) == 0:
        f = 0

    else:
        f = 1

    if f == 1:
        print(2)

    else:
        print(1)


t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    if sorted(a)==a:
        print(0)
        continue
    else:
        cnt=0
        for i in range(len(a)):
            if i+1==a[i]:
                cnt+=1
        if cnt==0:
            print(1)
        else:
            print(2)


# input=__import__('sys').stdin.readline
# s=list(input())
# print(s)
# sums=[0]*len(s)
# for i in range(1,len(s)):
#     if s[i]==s[i-1]:
#         sums[i]=sums[i-1]+1
#     else:
#         sums[i]=sums[i-1]
# m=int(input())
# for i in range(m):
#     l,r=map(int,input().split())
#     print(sums[r-1]-sums[l-1])\
t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    if sorted(a)==a:
        print(0)
        continue
    else:
        cnt=0
        for i in range(len(a)):
            if i+1==a[i]:
                cnt+=1
        firstwrong=0
        secondwrong=0
        for i in range(len(a)):
            if (i+1)!=a[i]:
                firstwrong=min(firstwrong,i)
                secondwrong=max(secondwrong,i)
        newcnt=0
        for i in range(firstwrong,secondwrong+1):
            if i+1==a[i]:
                newcnt+=1

        if cnt==0 or newcnt==0:
            print(1)
        else:
            print(2)


t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    if sorted(a)==a:
        print(0)
        continue
    else:
        cnt=0
        for i in range(len(a)):
            if i+1==a[i]:
                cnt+=1
        if cnt>1:
            print(2)
        else:
            print(1)


t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    if sorted(a)==a:
        print(0)
        continue
    else:
        cnt=0
        for i in range(len(a)):
            if i+1==a[i]:
                cnt+=1
        firstwrong=n+1
        secondwrong=0
        for i in range(len(a)):
            if (i+1)!=a[i]:
                firstwrong=min(firstwrong,i)
                secondwrong=max(secondwrong,i)
        newcnt=0
        for i in range(firstwrong,secondwrong+1):
            if i+1==a[i]:
                newcnt+=1

        if cnt==0 or newcnt==0:
            print(1)
        else:
            print(2)


t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    if sorted(a)==a:
        print(0)
        continue
    else:
        cnt=0
        for i in range(len(a)):
            if i+1==a[i]:
                cnt+=1
        firstwrong=0
        secondwrong=0
        for i in range(len(a)):
            if (i+1)!=a[i]:
                firstwrong=min(firstwrong,i)
                secondwrong=max(secondwrong,i)
        newcnt=0
        for i in range(firstwrong,secondwrong+1):
            if i+1==a[i]:
                newcnt+=1

        if cnt==0 or newcnt==0 or (cnt==0 and newcnt==0):
            print(1)
        else:
            print(2)


import sys
LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split()))
MI=lambda:map(int, sys.stdin.readline().strip('\n').split())
SI=lambda:sys.stdin.readline().strip('\n')
II=lambda:int(sys.stdin.readline().strip('\n'))

for _ in range(II()):
	n, a, mx=II(), LI()+[n+1], 0
	ans={0:1, 1:0, 2:0}
	for i, v in enumerate(a):
		if i==v-1 and mx>v:
			ans[2]=3
		if i and a[i-1]!=a[i]-1:
			if ans[1]:
				ans[2]=3
			ans[1]=2
		mx=max(mx, v)
	print(max(ans, key=lambda x:ans[x]))


import sys
LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split()))
MI=lambda:map(int, sys.stdin.readline().strip('\n').split())
SI=lambda:sys.stdin.readline().strip('\n')
II=lambda:int(sys.stdin.readline().strip('\n'))

for _ in range(II()):
	n, a, mx=II(), LI(), 0
	ans={0:1, 1:0, 2:0}
	for i, v in enumerate(a):
		if i==v-1 and mx>v:
			ans[2]=3
		if i and a[i-1]!=a[i]-1:
			ans[1]=2
		mx=max(mx, v)
	print(max(ans, key=lambda x:ans[x]))


import sys
LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split()))
MI=lambda:map(int, sys.stdin.readline().strip('\n').split())
SI=lambda:sys.stdin.readline().strip('\n')
II=lambda:int(sys.stdin.readline().strip('\n'))

for _ in range(II()):
	n=II()
	a=LI()+[n+1]
	ans, p=0, 0
	for c, v in enumerate(a, 1):
		if c==v:
			if c-p>1:
				ans+=1
			p=c
	print(min(2, ans))


import sys
LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split()))
MI=lambda:map(int, sys.stdin.readline().strip('\n').split())
SI=lambda:sys.stdin.readline().strip('\n')
II=lambda:int(sys.stdin.readline().strip('\n'))

for _ in range(II()):
	n=II()
	a=LI()
	mx=0
	ans={0:1, 1:0, 2:0}
	for i, v in enumerate(a):
		if i==v-1 and (mx>v or ans[1]):
			ans[2]=3
		if i and a[i-1]!=a[i]-1:
			ans[1]=2
		mx=max(mx, v)
	print(max(ans, key=lambda x:ans[x]))


import sys
LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split()))
MI=lambda:map(int, sys.stdin.readline().strip('\n').split())
SI=lambda:sys.stdin.readline().strip('\n')
II=lambda:int(sys.stdin.readline().strip('\n'))

for _ in range(II()):
	n, cnt=II(), 0
	for i, v in enumerate(MI(), 1):
		if i==v:
			cnt+=1
	print(0 if cnt==n else (1 if cnt==0 else 2))


import sys
LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split()))
MI=lambda:map(int, sys.stdin.readline().strip('\n').split())
SI=lambda:sys.stdin.readline().strip('\n')
II=lambda:int(sys.stdin.readline().strip('\n'))

for _ in range(II()):
	n=II()
	a=[0]+LI()+[n+1]
	ind=[]
	for i, v in enumerate(a):
		if i==v:
			ind.append(i)
	ans=0
	for i in range(1, len(ind)):
		if ind[i]-ind[i-1]>1:
			ans+=1
	print(2 if ans>1 else ans)


import sys
LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split()))
MI=lambda:map(int, sys.stdin.readline().strip('\n').split())
SI=lambda:sys.stdin.readline().strip('\n')
II=lambda:int(sys.stdin.readline().strip('\n'))

for _ in range(II()):
	n, a, sub, l=II(), LI(), 0, 0
	for i, v in enumerate(a):
		if i==v-1 and i and a[i-1]!=a[i]-1:
			sub+=1
			l=i+1
	if l and l!=n:
		sub+=1
	print(2 if sub>1 else sub)


import sys
LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split()))
MI=lambda:map(int, sys.stdin.readline().strip('\n').split())
SI=lambda:sys.stdin.readline().strip('\n')
II=lambda:int(sys.stdin.readline().strip('\n'))

for _ in range(II()):
	n=II()
	a=LI()+[n+1]
	mx=0
	ans={0:1, 1:0, 2:0}
	for i, v in enumerate(a):
		if i==v-1 and mx>v:
			ans[2]=3
		if i and a[i-1]!=a[i]-1:
			if ans[1]:
				ans[2]=3
			ans[1]=2
		mx=max(mx, v)
	print(max(ans, key=lambda x:ans[x]))


import sys
LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split()))
MI=lambda:map(int, sys.stdin.readline().strip('\n').split())
SI=lambda:sys.stdin.readline().strip('\n')
II=lambda:int(sys.stdin.readline().strip('\n'))

for _ in range(II()):
	n=II()
	a=LI()+[n+1]
	mx=0
	ans={0:1, 1:0, 2:0}
	for i, v in enumerate(a):
		if i==v-1 and (mx>v or ans[1]):
			ans[2]=3
		if i and a[i-1]!=a[i]-1:
			ans[1]=2
		mx=max(mx, v)
	print(max(ans, key=lambda x:ans[x]))


import sys
LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split()))
MI=lambda:map(int, sys.stdin.readline().strip('\n').split())
SI=lambda:sys.stdin.readline().strip('\n')
II=lambda:int(sys.stdin.readline().strip('\n'))

for _ in range(II()):
	n, a, sub=II(), LI(), 0
	for i, v in enumerate(a):
		if i==v-1 and i and a[i-1]!=a[i]-1:
			sub+=1
	print(2 if sub>1 else sub)


t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))

    if a == sorted(a):
        print(0)
    elif a == sorted(a, reverse=True) and n%2 == 0:
        print(1)
    else:
        print(2)


t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))

    all_rev = True
    for i in range(n):
        if a[i] == i+1:
            all_rev = False
            break

    if a == sorted(a):
        print(0)
    elif all_rev:
        print(1)
    else:
        print(2)


t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))

    all_rev = True
    for i in range(n):
        if a[i] == i+1:
            all_rev = False
            break

    if a == sorted(a):
        print(0)
    else:
        ct = 0
        i = 0
        while i < n:
            j = i+1
            if a[i] != i+1:
                while j < n and j != a[j]-1:
                    j += 1
                ct += 1

            i = j

        if ct == 1:
            print(1)
        else:
            print(2)


t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    ct = 0
    i = 0
    while i < n:
        j = i+1
        if a[i] != i+1:
            while j < n and j != a[j]-1:
                j += 1
            ct += 1

        i = j
    print(ct)


t = int(input())

for _ in range(t):
  n = int(input())
  a = list(map(int, input().split()))

  to_s = 0
  p = 0
  num = 1
  is_ = False
  while p < n:
    #print(p)
    if a[p] != num and (not is_):
      to_s += 1
      is_ = True
    if is_ and a[p] == num:
      is_ = False
    num+=1
    p+=1
  print(min(to_s, 2))


t = int(input())

for _ in range(t):
  n = int(input())
  a = list(map(int, input().split()))

  to_s = 0
  for i in range(len(a)):
    if a[i] == i+1: to_s +=1
  if to_s == 0: print(1)
  if to_s == n: print(0)
  else: print(2)


t = int(input())

for _ in range(t):
  n = int(input())
  a = list(map(int, input().split()))

  to_s = 0
  for i in range(len(a)):
    if a[i] == i+1: to_s +=1
  #print(to_s, n)
  if to_s == 0: print(1)
  elif to_s == n: print(0)
  else: print(2)


t = int(input())

for _ in range(t):
  n = int(input())
  a = list(map(int, input().split()))

  to_s = 0
  for i in range(len(a)):
    if a[i] == i+1: to_s +=1
  #print(to_s, n)
  if to_s == 0: print(2)
  elif to_s == n: print(0)
  else: print(1)


t = int(input())

for _ in range(t):
  n = int(input())
  a = list(map(int, input().split()))

  to_s = 0
  p = 0
  num = 1
  is_ = False
  while p < n:
    #print(p)
    if a[p] != num and (not is_):
      to_s += 1
      is_ = True
    if is_ and p+1 == num:
      is_ = False
    num+=1
    p+=1
  print(min(to_s, 2))


from sys import stdin

for _ in range(int(stdin.readline())):
    length = int(stdin.readline())
    array = list(map(int, stdin.readline().split()))
    count = 0
    start = 0
    end = 0
    for i in range(length):
        if array[i] != i + 1:
            start = i
            break
    for i in range(length - 1, -1, -1):
        if array[i] != i + 1:
            end = i
            break
    if start > end:
        print(0)
    else:
        for i in range(start, end + 1):
            if array[i] == i + 1:
                count += 1
        if count == 0:
            print(0)
        elif count == (end - start + 1):
            print(1)
        else:
            print(0)


from sys import stdin

for _ in range(int(stdin.readline())):
    length = int(stdin.readline())
    array = list(map(int, stdin.readline().split()))
    count = 0
    start = length
    end = 0
    for i in range(length):
        if array[i] != i + 1:
            start = i
            break
    for i in range(length - 1, -1, -1):
        if array[i] != i + 1:
            end = i
            break
    if start > end:
        print(0)
    else:
        for i in range(start, end + 1):
            if array[i] == i + 1:
                count += 1
        if count == 0:
            print(0)
        elif count == (end - start + 1):
            print(1)
        else:
            print(2)


from sys import stdin

for _ in range(int(stdin.readline())):
    length = int(stdin.readline())
    array = list(map(int, stdin.readline().split()))
    count = 0
    for i in range(length):
        if array[i] == i + 1:
            count += 1
    if count == 0:
        print(1)
    elif count == length:
        print(0)
    else:
        print(2)


from sys import stdin

for _ in range(int(stdin.readline())):
    length = int(stdin.readline())
    array = list(map(int, stdin.readline().split()))
    count = 0
    start = length
    end = 0
    for i in range(length):
        if array[i] != i + 1:
            start = i
            break
    for i in range(length - 1, -1, -1):
        if array[i] != i + 1:
            end = i
            break
    if start > end:
        print(0)
    else:
        for i in range(start, end + 1):
            if array[i] == i + 1:
                count += 1
        if count == 0:
            print(1)
        elif count == (end - start + 1):
            print(0)
        else:
            print(2)


from sys import stdin

for _ in range(int(stdin.readline())):
    length = int(stdin.readline())
    array = list(map(int, stdin.readline().split()))
    count = 0
    start = length
    end = 0
    for i in range(length):
        if array[i] != i + 1:
            start = i
            break
    for i in range(length - 1, -1, -1):
        if array[i] != i + 1:
            end = i
            break
    if start > end:
        print(0)
    else:
        for i in range(start, end + 1):
            if array[i] == i + 1:
                count += 1
        if count == 0:
            print(0, _, start, end, count)
        elif count == (end - start + 1):
            print(1)
        else:
            print(2)


for _ in range(int(input())):
    n = int(input())
    arr = list(map(int,input().split()))
    count = 0
    matching = True
    for i in range(len(arr)):
        if arr[i]!=i+1:
            if matching:
                count+=1
            matching = False
        else:
            matching = True
    print(min(count,2))


st = str(raw_input())
n = int(raw_input())
size = len(st)/n
def reverse_string(a_string):
    return a_string[::-1]
cnt = 0
for i in range(0,len(st),size):
    if st[i:size+i] == "".join(reversed(str(st[i:size+i]))):
        cnt += 1

if cnt == n:
    print "YES"
else:
    print "NO"


st = str(raw_input())
n = int(raw_input())
size = len(st)/n
def is_palindrome(seq):
    return seq == seq[::-1]
if n <= len(st):
 cnt = 0
 for i in range(0,len(st),size):
    if  is_palindrome(st[i:size+i].lower()):
        cnt += 1
    else:
        cnt -=1

 if cnt == n:
    print "YES"
 else:
    print "NO"
else:
    print "NO"


st = str(raw_input())
n = int(raw_input())
size = len(st)/n
def reverse_string(a_string):
    return a_string[::-1]
cnt = 0
for i in range(0,len(st),size):
    print st[i:size+i],"".join(reversed(str(st[i:size+i])))
    if st[i:size+i].lower() == "".join(reversed(str(st[i:size+i]))).lower():
        cnt += 1

if cnt == n:
    print "YES"
else:
    print "NO"


st = str(raw_input())
n = int(raw_input())
size = len(st)/n
def reverse_string(a_string):
    return a_string[::-1]
cnt = 0
for i in range(0,len(st),size):
    if st[i:size+i].lower() == "".join(reversed(str(st[i:size+i]))).lower():
        cnt += 1

if cnt == n:
    print "YES"
else:
    print "NO"


st = str(raw_input())
n = int(raw_input())
size = len(st)/n
def reverse_string(a_string):
    return a_string[::-1]
cnt = 0
for i in range(0,len(st),size):
    if st[i:size+i].lower() == reverse_string(str(st[i:size+i])).lower():
        cnt += 1

if cnt == n:
    print "YES"
else:
    print "NO"


s = raw_input()
n = len(s)
for i in xrange(1, n + 1):
    if n % i == 0:
        for j in xrange(0, n, i):
            if s[j:j + i] != s[j:j + i][::-1]:
                break
        else:
            print "YES"
            break
else:
    print "NO"


s = raw_input()
k = int(raw_input())
n = len(s)
if n % k != 0:
    print "NO"
else:
    for i in xrange(0, n, k):
        if s[i:i + k] != s[i:i + k][::-1]:
            print "NO"
            break
    else:
        print "YES"


s = raw_input()
k = int(raw_input())
n = len(s)
if n % k != 0:
    print "NO"
else:
    d = n / k
    for i in xrange(0, n, d):
        if s[i:i + d] != s[i:i + d][::-1]:
            print "NO"
            break
    else:
        print "YES"


def isPal(s):
	a = [x for x in s]
	b = [x for x in s]
	b.reverse()
	return a == b

def f(s,k,n):
	if n % k != 0:
		return 'NO'
	l = n / k
	for i in range(l):
		if not isPal(s[i * l : (i + 1) * l]):
			return 'NO'
	return 'YES'

s = raw_input()
k = int(raw_input())
n = len(s)
print f(s,k,n)


def isPal(s):
	a = [x for x in s]
	b = [x for x in s]
	b.reverse()
	return a == b

def f(s,k,n):
	if n % k != 0:
		return 'NO'
	l = n / k
	for i in range(l):
		if not isPal(s[i * k : (i + 1) * k]):
			return 'NO'
	return 'YES'

s = raw_input()
k = int(raw_input())
n = len(s)
print f(s,k,n)


def isPal(s):
	a = [x for x in s]
	b = [x for x in s]
	b.reverse()
	return a == b

def f(s,k,n):
	if n % k != 0:
		return 'NO'
	l = n / k
	for i in range(k):
		if not isPal(s[i * l : (i + 1) * l]):
			return 'NO'
	return 'YES'

s = raw_input()
k = int(raw_input())
n = len(s)
print f(s,k,n)


t=raw_input()
n=input()
L=len(t)
l=L/n
flag=0
i=0
j=l
while(j<=L):
    t1=t[i:j]
    t2=t1[::-1]
    if t1==t2:
        flag=1
    else:
        flag=0
        break
    i=j
    j=j+l
if(flag==1):
    print "YES"
else:
    print "NO"


t=raw_input()
n=input()
L=len(t)
l=L/n
flag=0
i=0
j=l
if L%n!=0:
    j=L+1
while(j<=L):
    t1=t[i:j]
    t2=t1[::-1]
    if t1==t2:
        flag=1
    else:
        flag=0
        break
    i=j
    j=j+l
if(flag==1):
    print "YES"
else:
    print "NO"


def  A():
    s=raw_input()
    k=int(raw_input())
    if len(s)%k!=0:
        return "NO"
    for i in xrange(0, len(s), len(s)/k):
        p=s[i:i+(len(s)/k)]
        print p
        if p!=p[::-1]:
            return "NO"
    return "YES"
print A()


def  A():
    s=raw_input()
    k=int(raw_input())
    if len(s)%k!=0:
        return "NO"
    for i in xrange(0, len(s), len(s)/k):
        p=s[i:i+(len(s)/k)]
        if p!=p[::-1]:
            return "NO"
    return "YES"
print A()


data=raw_input()
n=int(raw_input())
chk=0
length=len(data)/n

for i in range(n):
	tmp=data[i*length:i*length+length]
	for i in range(len(tmp)):
		if tmp[i]!=tmp[len(tmp)-i-1]:
			chk=1
			break
	if chk==1:
		print 'NO'
		break

if chk==0:
	print 'YES'


data=raw_input()
n=int(raw_input())
chk=0
length=len(data)/n

if len(data)%n!=0:
	print 'NO'
	exit(0)

for i in range(n):
	tmp=data[i*length:i*length+length]
	for i in range(len(tmp)):
		if tmp[i]!=tmp[len(tmp)-i-1]:
			chk=1
			break
	if chk==1:
		print 'NO'
		break

if chk==0:
	print 'YES'


def main():
    text = raw_input()
    m = int(raw_input())
    length = len(text)
    if length % m:
        print "NO"
        return
    blocks = [text[i, i+m] for i in range(0, length, m)]
    err_str = filter(lambda s: True if s != s[::-1] else False, blocks)
    if len(err_str):
        print "NO"
    else:
        print "YES"


text = raw_input()
m = int(raw_input())
length = len(text)
if length % m:
    print "NO"
else:
    blocks = [text[i:i+length/m] for i in range(0, length, length/m)]
    err_str = filter(lambda s: True if s != s[::-1] else False, blocks)
    print "NO" if len(err_str) else "YES"


text = raw_input()
m = int(raw_input())
length = len(text)
blocks = [text[i:i+length/m] for i in range(0, length, length/m)]
err_str = filter(lambda s: True if s != s[::-1] else False, blocks)
print "NO" if (length%m or len(err_str)) else "YES"


text = raw_input()
m = int(raw_input())
length = len(text)
blocks = [text[i:i+length/m] for i in range(0, length, length/m)]
err_str = filter(lambda s: True if s != s[::-1] else False, blocks)
print "NO" if len(err_str) else "YES"


text = raw_input()
m = int(raw_input())
length = len(text)
blocks = [text[i:i+length/m] for i in range(0, length, length/m)]
err_str = filter(lambda s: True if s != s[::-1]
print "NO" if （length%m or len(err_str)） else "YES"


text = raw_input()
m = int(raw_input())
length = len(text)
blocks = [text[i:i+length/m] for i in range(0, length, length/m)]
err_str = filter(lambda s: True if s != s[::-1]
print "NO" if length%m or len(err_str) else "YES"


def main():
    text = raw_input()
    m = int(raw_input())
    length = len(text)
    if length % m:
        print "NO"
        return
    blocks = [text[i:i+length/m] for i in range(0, length, length/m)]
    err_str = filter(lambda s: True if s != s[::-1] else False, blocks)
    if len(err_str): print "NO"
    else: print "YES"

main()


import math

st=raw_input()
k=int(raw_input())
fg=0
pl=int(math.ceil(1.0*len(st)/k))
le=len(st)
cnt=0
print pl
for i in range(k):
  pch= st[i*pl:i*pl+pl]
  #print pch
  if pch==pch[::-1]:
     cnt+=1
if cnt==k:
	print  "YES"
else:
	print "NO"


import math
import sys
st=raw_input()
k=int(raw_input())
fg=0
pl=int((len(st)/k))
le=len(st)
cnt=0
#print pl
if pl==0 or len(st)%k!=0:
	print "NO"
	sys.exit(0)
for i in range(k):
  pch= st[i*pl:i*pl+pl]
  #print pch
  if pch==pch[::-1]:
     cnt+=1
if cnt==k:
	print  "YES"
else:
	print "NO"


import math

st=raw_input()
k=int(raw_input())
fg=0
pl=int(math.ceil(1.0*len(st)/k))
le=len(st)
cnt=0
#print pl
for i in range(k):
  pch= st[i*pl:i*pl+pl]
  #print pch
  if pch==pch[::-1]:
     cnt+=1
if cnt==k:
	print  "YES"
else:
	print "NO"


#coding: utf-8

word = raw_input()
k = int(raw_input())
word_list = []
control = False
if(len(word)%k==0):
	for i in xrange(0, len(word), len(word)/k):
		word_list.append(word[i : i+(len(word)/k):])

for w in word_list:
	if (w[::]==w[::-1]):
		control = True
	else:
		control = False
		break

if(control):
	print "YES"
else:
	print "NO"


#coding: utf-8

word = raw_input()
k = int(raw_input())
control = False
if(len(word)%k==0):
	for i in xrange(0, len(word), len(word)/k):
		s=''
		if(i==0):
			s = word[i+(len(word)/k)-1 :  : -1]
		else:
			s = word[i+(len(word)/k)-1 : i-1 : -1]

		if (word[i : i+(len(word)/k):] == s):
			control=True
		else:
			control=False
			break

if(control):
	print "YES"
else:
	print "NO"


s = raw_input()
k = input()
c = len(s)/k
print "YES" if (c*k==len(s)) and (sum([s[c*i:c*(i+1)]==s[c*i:c*(i+1)][::-1] for i in xrange(k)])==k) else "NO"


s = raw_input()
k = input()
c = len(s)/k
print "YES" if (c*k==s) and (sum([s[c*i:c*(i+1)]==s[c*i:c*(i+1)][::-1] for i in xrange(k)])==k) else "NO"


s = raw_input()
k = input()
c = len(s)/k
print "YES" if sum([s[c*i:c*(i+1)]==s[c*i:c*(i+1)][::-1] for i in xrange(k)])==k else "NO"


S = raw_input()
K = int(raw_input())

if len(S) % K > 0:
	print "NO"
	exit()

def checkPalindome(S):
	for i in xrange(len(S)):
		if S[i] != S[len(S) - i - 1]:
			return False
	return True

J = len(S) / K
for i in xrange(J):
	if not checkPalindome(S[i * J:(i + 1) * J]):
		print "NO"
		exit()
print "YES"


S = raw_input()
K = int(raw_input())

if len(S) % K > 0:
	print "NO"
	exit()

def checkPalindome(S):
	for i in xrange(len(S)):
		if S[i] != S[len(S) - i - 1]:
			return False
	return True

J = len(S) / K
for i in xrange(K):
	if not checkPalindome(S[i * J:(i + 1) * J]):
		print "NO"
		exit()
print "YES"


"""
    Author: Hemanth Kumar Veeranki
    Handle:harry7

"""
#!/usr/bin/python
s=raw_input()
n=input()
p=len(s)/n
i=0
fl=True
while i<len(s):
    t=s[i:i+p]
    if(t!=t[::-1]):
        fl=False
        break
    i+=p
if(fl):print "YES"
else:print "NO"


"""
    Author: Hemanth Kumar Veeranki
    Handle:harry7

"""
#!/usr/bin/python
s=raw_input()
n=input()
p=len(s)/n
i=0
fl=True
if(len(s)%n!=0 or n>len(s)):fl=False
else:
    while i<len(s):
        t=s[i:i+p]
        if(t!=t[::-1]):
            fl=False
            break
        i+=p
if(fl):print "YES"
else:print "NO"


"""
    Author: Hemanth Kumar Veeranki
    Handle:harry7

"""
#!/usr/bin/python
s=raw_input()
n=input()
p=len(s)/n
i=0
fl=True
if(len(s)%n!=0):fl=False
while i<len(s):
    t=s[i:i+p]
    if(t!=t[::-1]):
        fl=False
        break
    i+=p
if(fl):print "YES"
else:print "NO"


"""
    Author: Hemanth Kumar Veeranki
    Handle:harry7

"""
#!/usr/bin/python
s=raw_input()
n=input()
p=len(s)/n
i=0
fl=True
if(len(s)%n!=0||n>len(s)):fl=False
else:
    while i<len(s):
        t=s[i:i+p]
        if(t!=t[::-1]):
            fl=False
            break
        i+=p
if(fl):print "YES"
else:print "NO"


s=raw_input()
n=len(s)
k=input()
def ispal(s):
    f=True
    for i in range(len(s)):
        if s[i]!=s[-(i+1)]:
            f=False
    return f
flag=True
if len(s)%k!=0:
    result = "NO"
else:
    for i in range(k):
        if not ispal(s[:n/k]):
            flag=False

        s=s[n/k:]
    if flag:
        result= "YES"
    else:
        result= "NO"
print result


s=raw_input()
n=len(s)
k=input()
def ispal(s):
    f=True
    for i in range(len(s)):
        if s[i]!=s[-(i+1)]:
            f=False
    return f
flag=True
if len(s)%k!=0:
    result = "NO"
else:
    for i in range(k):
        if not ispal(s[:n/k]):
            flag=False

        s=s[n/k:]
if flag:
    print "YES"
else:
    print "NO"


s = raw_input()
k = int(raw_input())

length = len(s)
num_palindromes = length/k

palindrome = True

s1 = ""
for i in range(length):
	s1 += s[i]
	if(len(s1) == num_palindromes):
		palindrome = (s1[::] == s1[::-1])
		s1 = ""

if palindrome and length % k == 0: print "YES"
else : print "NO"


s = raw_input()
k = int(raw_input())

length = len(s)
num_palindromes = length/k

palindrome = True

j = 1
i = 0
while i < length:
	palindrome = (s[i:num_palindromes*j:] == s[num_palindromes*j:i-1:-1])
	i += num_palindromes
	j += 1

if palindrome: print "YES"
else : print "NO"


s = raw_input()
k = int(raw_input())

length = len(s)
num_palindromes = length/k

palindrome = True

j = 1
i = 0
while i < length and j < length:
	palindrome = (s[i:num_palindromes*j:] == s[num_palindromes*j:i-1:-1])
	i += num_palindromes
	j += 1

if palindrome and length % k == 0: print "YES"
else : print "NO"


s = raw_input()
k = int(raw_input())

length = len(s)
num_palindromes = length/k

palindrome = True

s1 = ""
for i in range(length):
	s1 += s[i]
	if(len(s1) == num_palindromes):
		palindrome = (s1[::] == s1[::-1])
		if palindrome == False: break
		s1 = ""

if palindrome and length % k == 0: print "YES"
else : print "NO"


s = raw_input()
k = int(raw_input())

length = len(s)
num_palindromes = length/k

palindrome = True

j = 1
i = 0
while i < length:
	palindrome = (s[i:num_palindromes*j:] == s[num_palindromes*j:i-1:-1])
	i += num_palindromes
	j += 1

if palindrome and length % k == 0: print "YES"
else : print "NO"


s = raw_input()
k = int(raw_input())
a = len(s) / k
count = 1
b = 0
while count <= k:
	s_1 = s[(a * (count ** (count-1) -1)):(a * count):]
	s_2 = s_1[::-1]
	if s_1 == s_2:
		b += 1
	count += 1
if b == k:
	print "YES"
else:
	print "NO"


s = raw_input()
k = int(raw_input())
a = len(s) / k
count = 1
b = 0
while count <= k:
    s_1 = s[(a * count)-a:(a * count):]
    s_2 = s_1[::-1]
    if s_1 == s_2:
        b += 1
    count += 1
if b == k:
    print "YES"
else:
    print "NO"


s = raw_input()
k = float(raw_input())
a = len(s) / k
count = 1
b = 0
if a % 1 != 0:
    count = k+1
while count <= k:
    k = int(k)
    a = int(a)
    s_1 = s[(a * count)-a:(a * count):]
    s_2 = s_1[::-1]
    if s_1 == s_2:
        b += 1
    count += 1
if b == k:
    print "YES"
else:
    print "NO"


s = raw_input()
k = int(raw_input())
a = len(s) / k
count = 1
b = 0
while count <= k:
	s_1 = s[((a * (count)) -a):(a * count):]
	s_2 = s_1[::-1]
	if s_1 == s_2:
		b += 1
	count += 1
if b == k:
	print "YES"
else:
	print "NO"


string = raw_input()
k = raw_input()

count = len(string) % int(k)
keep = False
if (count == 0) :
  sum = count
  keep = True
  while (sum <= len(string) and keep):
    s = string[sum - len(string)/int(k):sum]
    if (s == s[::-1]):
      sum += len(string)/int(k)
    else:
      keep = False

if keep:
  print "YES"
else:
  print "NO"


string = raw_input()
k = raw_input()

count = len(string) % int(k)
keep = False
if (count == 0) :
  sum = count
  keep = True
  while (sum <= len(string) and keep):
    s = string[sum - len(string)/int(k):sum]
    if (s == s[::-1]):
      sum += len(string)/int(k)
    else:
      keep = False
else:
  print "NO"

if keep:
  print "YES"
else:
  print "NO"


string = raw_input()
k = raw_input()

count = len(string) % int(k)

if (count == 0) :
  sum = count
  keep = True
  while (sum <= len(string) and keep):
    s = string[sum - len(string)/int(k):sum]
    if (s == s[::-1]):
      sum += len(string)/int(k)
    else:
      keep = False
else:
  print "NO"

if keep:
  print "YES"
else:
  print "NO"


def palindrome(str):
    i = 0
    j = len(str)-1
    while(i<j):
        if(str[i]!=str[j]):
            return 0
        else:
            i=i+1
            j=j-1
    return 1

def splitCount(s, count):
     return [''.join(x) for x in zip(*[list(s[z::count]) for z in range(count)])]

string = raw_input()
k = int (raw_input())
if( (len(string) % k) == 0):
    string_len = (len(string))/k
    str = splitCount(string,string_len)
    i = 0
    flag = 0
    while (i < k):
        if(palindrome(str[i])):
            i=i+1
        else:
            print "NO"
            flag = 1
            break
    if(flag == 0):
        print "YES"
else:
    print "NO"


def palindrome(str):
    i = 0
    j = len(str)-1
    while(i<j):
        if(str[i]!=str[j]):
            return 0
        else:
            i=i+1
            j=j-1
    return 1

def splitCount(s, count):
     return [''.join(x) for x in zip(*[list(s[z::count]) for z in range(count)])]

string = "adabob"
k = 2
if( (len(string) % k) == 0):
    string_len = (len(string))/k
    str = splitCount(string,string_len)
    i = 0
    flag = 0
    while (i < k):
        if(palindrome(str[i])):
            i=i+1
        else:
            print "NO"
            flag = 1
            break
    if(flag == 0):
        print "YES"
else:
    print "NO"


def palindrome(str):
	i = 0
	j = len(str)-1
	while(i<j):
		if(str[i]!=str[j]):
			return 0
		else:
			i=i+1
			j=j-1
	return 1

def splitCount(s, count):
     return [''.join(x) for x in zip(*[list(s[z::count]) for z in range(count)])]

string = raw_input()
k = int (raw_input())
string_len = (len(string))/k
str = splitCount(string,string_len)
i = 0
flag = 0
while (i < k):
	if(palindrome(str[i])):
		i=i+1
	else:
		print "NO"
		flag = 1
		break
if(flag == 0):
	print "YES"


x = raw_input('')
y = float(raw_input(''))
z = int (len(x)/ y )
a = float(len(x)/ y)

bool = True
if len(x) > 1000 or len(x) < 1:
   raise SystemExit
if y > 1000 or y < 1:
    raise SystemExit

if ( a != z ):
    bool = False

i = 0
while bool and (y > i ):
    for j in range( z / 2):
        if x[(z * i ) + j] == x[(z * i + z ) -j - 1]:
                       bool = True
        else:
            bool = False

    i = i + 1
if bool:
    print 'YES'

else :
    print 'NO'


x = raw_input('')
y = int(raw_input(''))
z = len(x)/ y
a = float(len(x)/ y)
bool = True
if len(x) > 1000 or len(x) < 1:
   raise SystemExit
if y > 1000 or y < 1:
    raise SystemExit

if ( a != z ):
    bool = False
i = 0

while bool and (y > i ):
    for j in range( z / 2):
        if x[(z * i ) + j] == x[(z * i + z ) -j - 1]:
                       bool = True
        else:
            bool = False

    i = i + 1
if bool:
    print 'YES'

else :
    print 'NO'


x = raw_input('')
y = int(raw_input(''))
z = len(x)/ y
a = float(len(x)/ y)
bool = True
if ( a ! = z ):
    bool = False

if len(x) > 1000 or len(x) < 1:
   raise SystemExit
if y > 1000 or y < 1:
    raise SystemExit

i = 0
while bool and (y > i ):
    for j in range( z / 2):
        if x[(z * i ) + j] == x[(z * i + z ) -j - 1]:
                       bool = True
        else:
            bool = False

    i = i + 1
if bool:
    print 'YES'

else :
    print 'NO'


x = raw_input('')
y = int(raw_input(''))
z = len(x)/ y
a = float(len(x)/ y)
bool = True
if ( a != z ):
    bool = False

if len(x) > 1000 or len(x) < 1:
   raise SystemExit
if y > 1000 or y < 1:
    raise SystemExit

i = 0
while bool and (y > i ):
    for j in range( z / 2):
        if x[(z * i ) + j] == x[(z * i + z ) -j - 1]:
                       bool = True
        else:
            bool = False

    i = i + 1
if bool:
    print 'YES'

else :
    print 'NO'


x = raw_input('')
y = int(raw_input(''))
z = len(x)/ y

if len(x) > 1000 or len(x) < 1:
   raise SystemExit
if y > 1000 or y < 1:
    raise SystemExit
bool = True
i = 0
while bool and (y > i ):
    for j in range( z / 2):
        if x[(z * i ) + j] == x[(z * i + z ) -j - 1]:
                       bool = True
        else:
            bool = False

    i = i + 1
if bool:
    print 'YES'

else :
    print 'NO'


x = raw_input('')
y = int(raw_input(''))
z = len(x)/ y
a = float(len(x)/ y)
bool = True
if ( a ! = z )
bool = False

if len(x) > 1000 or len(x) < 1:
   raise SystemExit
if y > 1000 or y < 1:
    raise SystemExit

i = 0
while bool and (y > i ):
    for j in range( z / 2):
        if x[(z * i ) + j] == x[(z * i + z ) -j - 1]:
                       bool = True
        else:
            bool = False

    i = i + 1
if bool:
    print 'YES'

else :
    print 'NO'


x = raw_input('')
y = int(raw_input(''))
z = len(x)/ y
a = float(len(x)/ y)
bool = Trueif len(x) > 1000 or len(x) < 1:
   raise SystemExit
if y > 1000 or y < 1:
    raise SystemExit

if ( a != z ):
    bool = False

i = 0
while bool and (y > i ):
    for j in range( z / 2):
        if x[(z * i ) + j] == x[(z * i + z ) -j - 1]:
                       bool = True
        else:
            bool = False

    i = i + 1
if bool:
    print 'YES'

else :
    print 'NO'


s = raw_input()
k = int(raw_input())
flag = 0
i= 0
while i< len(s):
	t = s[(i):(i+len(s)/k)][::-1]
	#print t
	if t!=s[i:i+len(s)/k]:
		flag = 1
	i+=len(s)/k
if flag == 1:
	print "NO"
else:
	print "YES"


s = raw_input()
k = int(raw_input())
flag = 0
i= 0
if len(s) % k != 0:
	print "NO"
else:
	while i< len(s):
		t = s[(i):(i+len(s)/k)][::-1]
		#print t
		if t!=s[i:i+len(s)/k]:
			flag = 1
		i+=len(s)/k
	if flag == 1:
		print "NO"
	else:
		print "YES"


__author__ = 'Vishal Gupta'

def palin(string):
    p1 = 0
    p2 = len(string) - 1
    while(p1 < p2):
        if string[p1] == string[p2]:
            p1 += 1
            p2 -= 1
            continue
        else:
            return False
    return True

s = raw_input()
k = int(raw_input())
length = len(s) / k
initial = 0
end = length
break_true = True
for n in xrange(k):
    if (palin(s[initial:end])):
        initial += length
        end += length
        continue
    else:
        break_true = False
        break
if break_true:
    print "YES"
else:
    print "NO"


__author__ = 'Vishal Gupta'

def palin(string):
    p1 = 0
    p2 = len(string) - 1
    while(p1 < p2):
        if string[p1] == string[p2]:
            p1 += 1
            p2 -= 1
            continue
        else:
            return False
    return True

s = raw_input()
k = int(raw_input())
length = len(s) / k
initial = 0
end = length
break_true = True
for n in xrange(k):
    if (palin(s[initial:end])):
        initial += length
        end += length
        continue
    else:
        break_true = False
        break
if len(s) % k != 0:
    print "NO"
elif break_true:
    print "YES"
else:
    print "NO"


__author__ = 'Vishal Gupta'

def palin(string):
    p1 = 0
    p2 = len(string) - 1
    while(p1 < p2):
        if string[p1] == string[p2]:
            p1 += 1
            p2 -= 1
            continue
        else:
            return False
    return True

s = raw_input()
k = int(raw_input())
length = len(s) / k
initial = 0
end = length
break_true = True
for n in xrange(k):
    if (palin(s[initial:end])):
        initial += length
        end += length
        continue
    else:
        break_true = False
        break
if len(s) % k == 1:
    print "NO"
elif break_true:
    print "YES"
else:
    print "NO"


__author__ = 'Vishal Gupta'

def palin(string):
    p1 = 0
    p2 = len(string) - 1
    while(p1 < p2):
        if string[p1] == string[p2]:
            p1 += 1
            p2 -= 1
            continue
        else:
            return False
    return True

s = raw_input()
k = int(raw_input())
length = len(s) / k
initial = 0
end = length
break_true = True
for n in xrange(k):
    if (palin(s[initial:end])):
        initial += length
        end += length
        continue
    else:
        break_true = False
        break
if len(s) < k:
    print "NO"
elif break_true:
    print "YES"
else:
    print "NO"


__author__ = 'Vishal Gupta'

def palin(string):
    p1 = 0
    p2 = len(string) - 1
    while(p1 < p2):
        if string[p1] == string[p2]:
            p1 += 1
            p2 -= 1
            continue
        else:
            return False
    return True

s = raw_input()
k = int(raw_input())
length = len(s) / k
initial = 0
end = length
break_true = True
for n in xrange(k):
    if (palin(s[initial:end])):
        initial += length
        end += length
        continue
    else:
        break_true = False
        break
if len(s) % k == 1:
    print "NO"
elif k > len(s):
    print "NO"
elif break_true:
    print "YES"
else:
    print "NO"


INF = 999999999999999999L
EPS = 1e-12

def read():
    return raw_input().strip()

def read_ints():
    return map(int,read().split())

def is_pal(s):
    for i in xrange(len(s)/2):
        if s[i] != s[len(s)-1-i]:
            return False
    return True

s = read()
k = int(read())
if len(s)%k != 0:
    print "NO"
else:
    l = len(s)/k
    for i in xrange(k):
        if not is_pal(s[i*l:(i+1)*l]):
            print "NO"
            break
    else:
        print "YES"


def ispalin(s):
    return s==s[::-1]

s = raw_input()
k = input()
toggle = 0
i = 0
z = len(s)/k
if len(s)%k!=0:
    toggle = 1

if k>len(s):
    if not ispalin(s):
        toggle = 1
while i<=len(s)-z and k<len(s):

    if not ispalin(s[i:i+z]) and toggle==0:
        toggle = 1
        break
    i+=z

if toggle==1:
    print 'NO'
else:
    print 'YES'


def ispalin(s):
    return s==s[::-1]

s = raw_input()
k = input()
toggle = 0
i = 0
z = len(s)/k
if len(s)%k!=0:
    toggle = 1
while i<=len(s)-z:
    if not ispalin(s[i:i+z]) and toggle==0:
        toggle = 1
        break
    i+=z

if toggle==1:
    print 'NO'
else:
    print 'YES'


def is_palindrome(s):
	if(s==s[::-1]):
		return True
	else:
		return False
k=raw_input()
number = int(input())
t =True
i=0
#print len(k)/number
while(i<len(k)):
	t = t and is_palindrome(k[i:i+len(k)/number])
	i=i+len(k)/number
if(t):
	print "YES"
else:
	print "NO"


def is_palindrome(s):
	if(s==s[::-1]):
		return True
	else:
		return False
k=raw_input()
number = int(input())
t =True
i=0
f = True
if(number>len(k)):
	f = False
	print "NO"
if(f):
	while(i<len(k)):
		t = t and is_palindrome(k[i:i+len(k)/number])
		i=i+len(k)/number

	if(len(k)%number!=0):
		print "NO"
	elif(t):
		print "YES"
	else:
		print "NO"


def is_palindrome(s):
	if(s==s[::-1]):
		return True
	else:
		return False
k=raw_input()
number = int(input())
t =True
i=0
while(i<len(k)):
	t = t and is_palindrome(k[i:i+len(k)/number])
	i=i+len(k)/number
if(len(k)%number!=0):
	print "NO"
elif(t):
	print "YES"
else:
	print "NO"


def is_palindrome(s):
	if(s==s[::-1]):
		return True
	else:
		return False
k=raw_input()
number = int(input())
t =True
i=0
#print len(k)/number
while(i<len(k)):
	#print k[i:i+len(k)/number]
	t = t and is_palindrome(k[i:i+len(k)/number])
	i=i+len(k)/number
if(t):
	print "YES"
else:
	print "NO"


def ispal(s):
    return s == s[::-1]
s = raw_input()
k = int(raw_input())
if len(s) % k:
    print "NO"
else:
    m = len(s) / k
    ans = "YES"
    for i in xrange(0, len(s), m):
        if not ispal(s[i:i+m]):
            ans = "NO"
            break
    print ans


def ispal(s):
    return s == s[::-1]
s = raw_input()
k = int(raw_input())
m = len(s) / k
ans = "YES"
for i in xrange(0, len(s), m):
    if not ispal(s[i:i+m]):
        ans = "NO"
        break
print ans


#include<stdio.h>
#include<string.h>
int main()
{
    int k,i,j,n,lm,l;
    char mes[1010];
    scanf("%s",mes);
    scanf("%d",&k);
    l=strlen(mes);
    if(l%k!=0)
    {
        printf("NO");
        return 0;
    }
    lm=l/k;
    for(i=0;i<k;i++)
    {
        for(j=i*lm,n=((i+1)*(lm-1)+i);j<((i+1)*lm)/2,n>=(i*lm)+lm/2;j++,n--)
        {
            if(mes[j]!=mes[n])
            {
                printf("NO");
                return 0;
            }
        }
    }
    printf("YES");
}


#include<stdio.h>
#include<string.h>

int main()
{ char s[1005];
	int k,len,c,n,m=0,i,j;

		scanf("%s",s);
	scanf("%d",&k);

	len=strlen(s);
	c=(len)/k;

	while(m!=len)
	{ n=m;
	m=m+c;

	{for(i=n,j=(m-1);i<m;i++,j--)
	if(s[i]==s[j])
	{
	continue;}

	else
	{printf("NO");
	return 0;}
	}
	}
	printf("YES");

}


#include <stdio.h>
#include <string.h>

int main()
{
    char s[1010];
    int n,l,i,j;
    gets(s);
    scanf("%d",&n);

    if (strlen(s)% n > 0)
    {
        printf("NO\n");
        return 0;
    }

    l = strlen(s) / n;
    for (i = 0; i < n; i++)
        for (j = 0; j < l / 2; j++)
            if (s[j + i * l] != s[(i + 1) * l - j - 1])
            {
                printf("NO\n");
                return 0;
            }

    printf("YES\n");
    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int i,n,a=0,l,b,k,wl,j,m,cmp,count=0,g;
    char str[1001],temp[1001],temp2[1001];
    scanf("%s%d",str,&k);
    l=strlen(str);
    wl=l/k;
    if((k>l)||(wl*k!=l))
    {
        printf("NO");
        return 0;
    }
    for(i=0;i<l;i++)
    {
        for(j=0;j<wl;j++)
        {
            temp[j]=str[i+j];
        }
        temp[j]='\0';
        g=strlen(temp);
        for(j=g-1,m=0;m<g;j--,m++)
        {
            temp2[m]=temp[j];
        }
        temp2[m]='\0';
        cmp=strcmp(temp2,temp);
        if((cmp==0)&&(strlen(temp2)==wl))
        {
            if(count!=k)
            {
                count++;
                i+=wl-1;
            }

        }

    }
    if(count==k)
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }

    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int i,n,a=0,l,b,k,wl,j,m,cmp,count=0,g;
    char str[2001],temp[1001],temp2[1001];
    gets(str);
    l=strlen(str);
    scanf("%d",&k);
    wl=l/k;
    if((k>l)||(wl*k!=l))
    {
        printf("NO");
        return 0;
    }
    for(i=0;i<l;i++)
    {
        for(j=0;j<wl;j++)
        {
            temp[j]=str[i+j];
        }
        temp[j]='\0';
        g=strlen(temp);
        for(j=g-1,m=0;m<g;j--,m++)
        {
            temp2[m]=temp[j];
        }
        temp2[m]='\0';
        cmp=strcmp(temp2,temp);
        if(cmp==0)
        {
            if(count!=k)
            {
                count++;
            }

        }

    }
    if(count==k)
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }

    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int i,n,a=0,l,b,k,wl,j,m,cmp,count=0,g;
    char str[1000],temp[1000],temp2[1000];
    gets(str);
    l=strlen(str);
    scanf("%d",&k);
    wl=l/k;
    if(k>l)
    {
        printf("NO\n");
        return 0;
    }
    for(i=0;i<=l-wl;i++)
    {
        for(j=0;j<wl;j++)
        {
            temp[j]=str[i+j];
        }
        temp[j]='\0';
        g=strlen(temp);
        for(j=g-1,m=0;m<g;j--,m++)
        {
            temp2[m]=temp[j];
        }
        temp2[m]='\0';
        cmp=strcmp(temp2,temp);
        if(cmp==0)
        {
            count++;
        }

    }
    if(count==k)
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }

    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int i,n,a=0,l,b,k,wl,j,m,cmp,count=0,g;
    char str[1001],temp[1001],temp2[1001];
    gets(str);
    l=strlen(str);
    scanf("%d",&k);
    wl=l/k;
    if((k>l)||(wl*k!=l))
    {
        printf("NO");
        return 0;
    }
    for(i=0;i<l;i++)
    {
        for(j=0;j<wl;j++)
        {
            temp[j]=str[i+j];
        }
        temp[j]='\0';
        g=strlen(temp);
        for(j=g-1,m=0;m<g;j--,m++)
        {
            temp2[m]=temp[j];
        }
        temp2[m]='\0';
        cmp=strcmp(temp2,temp);
        if(cmp==0)
        {
            if(count!=k)
            {
                count++;
                i+=wl-1;
            }

        }

    }
    if(count==k)
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }

    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int i,n,a=0,l,b,k,wl,j,m,cmp,count=0,g;
    char str[1000],temp[1000],temp2[1000];
    gets(str);
    l=strlen(str);
    scanf("%d",&k);
    wl=l/k;
    if((k>l)||(wl*k!=l))
    {
        printf("NO");
        return 0;
    }
    for(i=0;i<l;i++)
    {
        for(j=0;j<wl;j++)
        {
            temp[j]=str[i+j];
        }
        temp[j]='\0';
        g=strlen(temp);
        for(j=g-1,m=0;m<g;j--,m++)
        {
            temp2[m]=temp[j];
        }
        temp2[m]='\0';
        cmp=strcmp(temp2,temp);
        if(cmp==0)
        {
            if(count!=k)
            {
                count++;
            }

        }

    }
    if(count==k)
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }

    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int i,n,a=0,l,b,k,wl,j,m,cmp,count=0,g;
    char str[1000],temp[1000],temp2[1000];
    gets(str);
    l=strlen(str);
    scanf("%d",&k);
    wl=l/k;
    if(k>l)
    {
        printf("NO\n");
        return 0;
    }
    for(i=0;i<=l-wl;i++)
    {
        for(j=0;j<wl;j++)
        {
            temp[j]=str[i+j];
        }
        temp[j]='\0';
        g=strlen(temp);
        for(j=g-1,m=0;m<g;j--,m++)
        {
            temp2[m]=temp[j];
        }
        temp2[m]='\0';
        cmp=strcmp(temp2,temp);
        if(cmp==0)
        {
            count++;
        }

    }
    if(count>=k)
    {
        printf("YES\n");
    }
    else
    {
        printf("NO");
    }

    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int i,n,a=0,l,b,k,wl,j,m,cmp,count=0,g;
    char str[1000],temp[1000],temp2[1000];
    gets(str);
    l=strlen(str);
    scanf("%d",&k);
    wl=l/k;
    for(i=0;i<=l-wl;i++)
    {
        for(j=0;j<wl;j++)
        {
            temp[j]=str[i+j];
        }
        temp[j]='\0';
        g=strlen(temp);
        for(j=g-1,m=0;m<g;j--,m++)
        {
            temp2[m]=temp[j];
        }
        temp2[m]='\0';
        cmp=strcmp(temp2,temp);
        if(cmp==0)
        {
            count++;
        }

    }
    if(count==k)
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }

    return 0;
}


#include <stdio.h>
#include <math.h>

int main()
{
    long long int i,n,a,b,c,j,k,sum=0;
    scanf("%lld %lld %lld",&a,&b,&c);
    for(i=1;i<=c;i++)
    {
        sum+=i*a;
    }

    b-=sum;
    if(b>=0)
    {
        printf("0");
    }
    else if(b<0)
    {
        b=abs(b);
        printf("%lld",b);
    }
    return 0;

}


#include <stdio.h>
#include <string.h>

int main()
{
    int i,n,a=0,l,b,k,wl,j,m,cmp,count=0,g;
    char str[1000],temp[1000],temp2[1000];
    gets(str);
    l=strlen(str);
    scanf("%d",&k);
    wl=l/k;
    if((k>l)||(wl*l!=k))
    {
        printf("NO\n");
        return 0;
    }
    for(i=0;i<=l-wl;i++)
    {
        for(j=0;j<wl;j++)
        {
            temp[j]=str[i+j];
        }
        temp[j]='\0';
        g=strlen(temp);
        for(j=g-1,m=0;m<g;j--,m++)
        {
            temp2[m]=temp[j];
        }
        temp2[m]='\0';
        cmp=strcmp(temp2,temp);
        if(cmp==0)
        {
            if(count!=k)
            {
                count++;
            }

        }

    }
    if(count==k)
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }

    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int i,n,a=0,l,b,k,wl,j,m,cmp,count=0,g;
    char str[1000],temp[1000],temp2[1000];
    gets(str);
    l=strlen(str);
    scanf("%d",&k);
    wl=l/k;
    for(i=0;i<=l-wl;i++)
    {
        for(j=0;j<wl;j++)
        {
            temp[j]=str[i+j];
        }
        temp[j]='\0';
        g=strlen(temp);
        for(j=g-1,m=0;m<g;j--,m++)
        {
            temp2[m]=temp[j];
        }
        temp2[m]='\0';
        cmp=strcmp(temp2,temp);
        if(cmp==0)
        {
            count++;
        }

    }
    if(count>=k)
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }

    return 0;
}


#include<stdio.h>
#include<string.h>

int main()
{
    char str[1002];
    gets(str);
    int n,l,x,sum=0,i=0,j,k,count=0;
    scanf("%d",&n);
    l=strlen(str);
    x=l/n;
    char ara1[x+1],ara2[x+1];

    if((l%n)==0)
    {

            for(;str[i]!='\0';)
            {
            for(j=0,k=x-1;j<x,k>=0;j++,k--,i++)
            {
                ara1[j]=str[i];
                ara2[k]=str[i];
            }
            for(j=0;j<x;j++)
            {
                if(ara1[j]==ara2[j])
                {
                    sum++;
                }
            }
            if(sum==x)
            {
                count++;
                sum=0;
            }

            }
        if(count==n)
        {
            printf("YES");
        }
        else
        {
            printf("NO");
        }
    }
    else
    {
        printf("NO");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int a,i,j,m,n,f,l,k,d,r;
    char c[10000];
    gets(c);
    scanf(" %d", &n);
    m=strlen(c);
    f=0;
    l=m/n;
    r=l;
    d=0;
    if(m%n==0)
    {
        for(k=0; k<n; k++)
        {

            for(i=f,j=l-1; i<(f+l)/2; i++,j--)
            {
                if(c[i]!=c[j])
                {
                    d=1;
                }
                if(d==1)
                {
                    printf("NO\n");
                    break;
                }
            }
            if(d==1)
            {
                break;
            }
            f=l;
            l=l+r;
        }
        if(d==0)
        {
            printf("YES\n");
        }
    }
    else
        printf("NO\n");
    return 0;
}


#include <stdio.h>

int main()
{
    int count = 0, check = 0, j = 0, k, lop, len = 0, t;
    char str[1000];
    scanf("%s", str);
    scanf("%d", &t);
    int i;
    for(i = 0; str[i] != '\0'; i++) {
        len++;
    }
    lop = len/t;
    k = j+lop-1;
    for(i = 1; i <= t; i++) {
        for(; count <= lop/2; j++, k--) {
            if(str[j] != str[k]) {
            check++;
            break;
            }
            count++;
        }
        if(check == 1) {
            break;
        }
        count = 0;
        j = i*lop;
        k = j + lop-1;
    }
    if(check == 1) {
        printf("NO");
    }
    else {
        printf("YES");
    }
    return 0;
}


#include <stdio.h>

int main()
{
    int count = 0, check = 0, j = 0, k, lop, len = 0, t;
    char str[1000];
    scanf("%s", str);
    scanf("%d", &t);
    int i;
    for(i = 0; str[i] != '\0'; i++) {
        len++;
    }
    if(len%t != 0) {
        check++;
    }
    lop = len/t;
    k = j+lop-1;
    for(i = 1; i <= t; i++) {
        for(; count <= lop/2; j++, k--) {
            if(str[j] != str[k]) {
            check++;
            break;
            }
            count++;
        }
        if(check >= 1) {
            break;
        }
        count = 0;
        j = i*lop;
        k = j + lop-1;
    }
    if(check >= 1) {
        printf("NO");
    }
    else {
        printf("YES");
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
    int length,j=0,q,i,k,n=0,p,w=1,x,l=0,m;
    char s[1001], word[1001],s2[1001];
    scanf("%s",&s);
    scanf("%d",&k);
    m=k;
    length=strlen(s);
    p=length/k;
    q=p*w;
    i=0;
    j=0;
    if(length%k!=0)
    {
        n=1;
    }
    for(;/*q<=length&&j<length&&*/j<q&&n!=1;j++)
    {
        word[i]=s[j];
        i++;
        if(j==(q)-1)
        {
            word[i]='\0';
            strcpy(s2,word);
            j=p-1;
            i=0;
            w++;
            q=p*w;
            strrev(word);
            x=strcmp(s2,word);
            if(x==0)
            {
                continue;
            }
            else
            {
                n=1;
                break;
            }
        }
    }

    if(n==1){
        printf("NO");
    }
    else{
        printf("YES");
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main()
{
    int length,j=0,q,i,k,n=0,p,w=1,x,l=0,m;
    char s[1002], word[1002],s2[1002];
    scanf("%s",&s);
    scanf("%d",&k);
    m=k;
    length=strlen(s);
    p=length/k;
    q=p*w;
    i=0;
    j=0;
    if(length%k!=0)
    {
        n=1;
    }
    for(;/*q<=length&&*/j<length&&n!=1;j++)
    {
        word[i]=s[j];
        i++;
        if(j==(q)-1)
        {
            word[i]='\0';
            strcpy(s2,word);
            j=p-1;
            i=0;
            w++;
            q=p*w;
            strrev(word);
            x=strcmp(s2,word);
            if(x==0)
            {
                continue;
            }
            else
            {
                n=1;
                break;
            }
        }
    }
    if(n==1){
        printf("NO");
    }
    else{
        printf("YES");
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main()
{
    int length,j=0,q,i,k,n=0,p,w=1,x,l=0,m;
    char s[1002], word[1002],s2[1002];
    scanf("%s",&s);
    scanf("%d",&k);
    m=k;
    length=strlen(s);
    p=length/k;
    q=p*w;
    i=0;
    j=0;
    if(length%k!=0)
    {
        n=1;
    }
    for(;j<length&&n!=1;j++)
    {
        word[i]=s[j];
        i++;
        if(j==(q)-1)
        {
            word[i]='\0';
            strcpy(s2,word);

            i=0;
            w++;
            j=p*(w-1)-1;
            q=p*w;
            strrev(word);
            x=strcmp(s2,word);
            if(x==0)
            {
                continue;
            }
            else
            {
                n=1;
                break;
            }
        }
    }
    if(n==1){
        printf("NO");
    }
    else{
        printf("YES");
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
int main()
{
    long long  int length,j=0,q,i,k,n=0,p,w=1,x,l=0,m;
    char s[1001], word[1001],s2[1001];
    scanf("%s",&s);
    scanf("%lld",&k);
    m=k;
    length=strlen(s);
    p=length/k;
    q=p*w;
    i=0;
    j=0;
    if(length%k!=0)
    {
        n=1;
    }

    for(;q<=length&&j<length&&j<q&&n!=1;j++)
    {
        word[i]=s[j];
        i++;
        if(j==(q)-1)
        {
            word[i]='\0';
            strcpy(s2,word);
            j=p-1;
            i=0;
            w++;
            q=p*w;
            strrev(word);
            x=strcmp(s2,word);
            if(x==0)
            {
                continue;
            }
            else
            {
                n=1;
                break;
            }
        }
    }

    if(n==1){
        printf("NO");
    }
    else{
        printf("YES");
    }
}