import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; int[] lds = new int[n + 1]; int maxD = 0; lds[0] = Integer.MAX_VALUE; int[] lis = new int[n + 1]; Arrays.fill(lis, Integer.MAX_VALUE); lis[0] = 0; int maxI = 0; int[] leftMaxesI = new int[n]; int[] leftMaxesD = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); for (int j = maxI; j >= 0; j--) { if (lis[j] < arr[i]) { lis[j + 1] = Math.min(lis[j + 1], arr[i]); maxI = Math.max(maxI, j + 1); break; } } leftMaxesI[i] = maxI; for (int j = maxD; j >= 0; j--) { if (lds[j] > arr[i]) { lds[j + 1] = Math.max(lds[j + 1], arr[i]); maxD = Math.max(maxD, j + 1); break; } } leftMaxesD[i] = maxD; } Arrays.fill(lds, 0); lds[0] = Integer.MAX_VALUE; Arrays.fill(lis, Integer.MAX_VALUE); lis[0] = 0; maxD = 0; maxI = 0; int ans = 0; for (int i = n - 1; i >= 0; i--) { for (int j = maxI; j >= 0; j--) { if (lis[j] < arr[i]) { lis[j + 1] = Math.min(lis[j + 1], arr[i]); maxI = Math.max(maxI, j + 1); break; } } ans = Math.max(ans, Math.min(leftMaxesI[i], maxI) - 1); for (int j = maxD; j >= 0; j--) { if (lds[j] > arr[i]) { lds[j + 1] = Math.max(lds[j + 1], arr[i]); maxD = Math.max(maxD, j + 1); break; } } ans = Math.max(ans, Math.min(leftMaxesD[i], maxD) - 1); } System.out.println(ans); } }