#include <bits/stdc++.h>
using namespace std;
int val[100500];
int main() {
  int n, cur;
  stack<int> st;
  int ans = 0;
  cin.sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) val[i] = -1;
  for (int i = 0; i < n; i++) {
    cin >> cur;
    int cv = 0;
    while (!st.empty() and st.top() < cur) {
      cv = max(cv, val[st.top()]);
      st.pop();
    }
    if (!st.empty()) {
      val[cur] = cv + 1;
      ans = max(ans, val[cur]);
    }
    st.push(cur);
  }
  cout << ans << endl;
  return 0;
}
