from bisect import bisect_left def solve(n,a): ans=0 dp_back=[10**20]*n b=[] for i in a[::-1]: dp_back[bisect_left(dp_back,i)]=i b.append(bisect_left(dp_back,10**20)) b=b[::-1] dp=[10**20]*n for i in range(n): dp[bisect_left(dp,a[i])]=a[i] ans=max(ans,b[i],bisect_left(dp,10**20)) return ans n=int(input()) a=list(map(int,input().split())) ans=solve(n,a) for i in range(n):a[i]*=-1 ans=max(ans,solve(n,a)) print(ans-1)