from bisect import bisect_left INF = 10 ** 10 ans = 0 n = int(input()) a = list(map(int, input().split())) ic = [0] * n dc = [0] * n dp = [INF] * (n + 1) dp[0] = -INF for i, x in enumerate(a): idx = bisect_left(dp, x) dp[idx] = x ic[i] = idx dp = [INF] * (n + 1) dp[0] = -INF for i, x in enumerate(a[::-1]): idx = bisect_left(dp, x) dp[idx] = x dc[n - i - 1] = idx for i in range(n): ans = max(ans, ic[i], dc[i]) a = list(map(lambda x: -x, a)) ic = [0] * n dc = [0] * n dp = [INF] * (n + 1) dp[0] = -INF for i, x in enumerate(a): idx = bisect_left(dp, x) dp[idx] = x ic[i] = idx dp = [INF] * (n + 1) dp[0] = -INF for i, x in enumerate(a[::-1]): idx = bisect_left(dp, x) dp[idx] = x dc[n - i - 1] = idx for i in range(n): ans = max(ans, ic[i], dc[i]) print(ans - 1)