import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) if A==list(sorted(A)): print(0) exit() down = [0, 0] for i in range(N-1): down.append(down[-1]+(1 if A[i]>A[i+1] else 0)) M = [0] for i in range(N): M.append(max(M[-1], A[i])) m = min(A) if down[N]==1: for i in range(N-1): if A[i]>A[i+1] and A[i+1]==m and A[-1]<=A[0]: print(1) exit() for i in range(N-2, 0, -1): if A[i]<=A[i+1]: if down[i]==1 and M[i]<=A[i]: print(1) exit() else: break print(2)