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])) if down[N]==1 and A[-1]==min(A): print(1) exit() for i in range(N-2, 0, -1): if A[i]<=A[i+1]: if down[i-1]==1 and M[i]<=A[i]: print(1) exit() else: break print(2)