from bisect import * def LIS(A): # weakly increase --> bisect_right # strictly increase --> bisect_left data = [] L = [] for i in range(N): ind = bisect_right(data, A[i]) flag = 0 if len(data) == ind: data.append(A[i]) flag = 1 else: data[ind] = A[i] if flag: L.append(len(data)) else: L.append(0) return L N = int(input()) A = list(map(int, input().split())) LU = LIS(A) A.reverse() RU = LIS(A) RU.reverse() A = list(map(lambda x : -x, A)) RD = LIS(A) RD.reverse() A.reverse() LD = LIS(A) ans = 0 for i in range(N): ans = max(ans, min(LU[i], RU[i])-1) ans = max(ans, min(LD[i], RD[i])-1) print(ans)