#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const int mod = 1000000007;
const long long INF = 1e18;
const int MX = 2000000001;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  long long t, q, n, a, b, k, l, m, x = 0, y = 0, ans = 0, temp = 0, res = 0;
  stack<long long> second;
  cin >> n;
  long long killer[n], dead[n], psycho[n];
  for (long long i = 0; i < (n); i++) {
    cin >> killer[i];
    dead[killer[i]] = i;
  }
  stack<pair<long long, long long> > st;
  for (long long i = 0; i < (n); i++) {
    if (st.size() == 0) {
      st.push({killer[i], -1});
      continue;
    }
    res = 0;
    while (st.size() and st.top().first < killer[i]) {
      res = max(res, st.top().second);
      st.pop();
    }
    if (st.size() == 0)
      st.push({killer[i], -1});
    else {
      st.push({killer[i], res + 1});
      ans = max(ans, res + 1);
    }
  }
  cout << ans;
}
