mod = 1000000007 eps = 10**-9 def main(): import sys from bisect import bisect_left input = sys.stdin.readline def solve(A): N = len(A) LIS = [-10**10] L = [0] * N for i, a in enumerate(A): j = bisect_left(LIS, a) if j == len(LIS): LIS.append(a) else: LIS[j] = a L[i] = j LIS_r = [-10**10] R = [0] * N A.reverse() for i, a in enumerate(A): j = bisect_left(LIS_r, a) if j == len(LIS_r): LIS_r.append(a) else: LIS_r[j] = a R[-1-i] = j ret = 0 for i in range(N): ret = max(ret, min(L[i], R[i]) - 1) return ret N = int(input()) A = list(map(int, input().split())) ans1 = solve(A) AA = [-a for a in A] ans2 = solve(AA) print(max(ans1, ans2)) if __name__ == '__main__': main()