#include <bits/stdc++.h>
using namespace std;
int st[100005];
int arr[100005], brr[100005];
int p = 0;
void f(int c) {
  st[p] = c;
  p++;
}
void cin_arr(int* arr, int n) {
  for (int i = 0; i < n; i++) cin >> arr[i];
}
int main() {
  int n;
  cin >> n;
  int ABS, j, ans = 0;
  ABS = j = n;
  cin_arr(arr, n);
  f(100005);
  for (int i = 0; i < n; i++) {
    brr[arr[n - i - 1]]++;
    while (arr[n - i - 1] > st[p - 1]) p--;
    f(arr[n - i - 1]);
    if (st[p - 1] == ABS) {
      p--;
      while (brr[j] != 0) j--;
      ABS = j;
    }
    ans = max(ans, p - 1);
  }
  cout << ans ? ans - 1 : 0;
}
