from bisect import bisect_left n = int(input()) a = list(map(int, input().split())) ans = 0 incl = [0] incli = [-1] for i, x in enumerate(a): p = bisect_left(incl, x) if p < len(incl): incl[p] = min(incl[p], x) else: incl.append(x) incli.append(i) incr = [0] incri = [n] for i, x in enumerate(reversed(a)): p = bisect_left(incr, x) if p < len(incr): incr[p] = min(incr[p], x) else: incr.append(x) incri.append(n-1-i) for i, (li, ri) in enumerate(zip(incli, incri)): if li <= ri: ans = max(ans, i) decl = [-10**9-1] decli = [-1] for i, x in enumerate(a): x = -x p = bisect_left(decl, x) if p < len(decl): decl[p] = min(decl[p], x) else: decl.append(x) decli.append(i) decr = [-10**9-1] decri = [n] for i, x in enumerate(reversed(a)): x = -x p = bisect_left(decr, x) if p < len(decr): decr[p] = min(decr[p], x) else: decr.append(x) decri.append(n-1-i) for i, (li, ri) in enumerate(zip(decli, decri)): if li <= ri: ans = max(ans, i) print(ans-1) #print(incli, incri) #print(decli, decri)