#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3fll;
const int LGN = 25;
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
int n;
struct LOL {
  int val[4 * N], lz[4 * N];
  void prop(int p, int L, int R) {
    if (!lz[p]) return;
    val[p] = lz[p];
    if (L < R) lz[2 * p] = lz[p], lz[2 * p + 1] = lz[p];
    lz[p] = 0;
  }
  void update(int i, int v, int p = 1, int L = 0, int R = n) {
    if (R < L || i < L) return;
    prop(p, L, R);
    if (R <= i) {
      val[p] = v;
      if (L < R) lz[2 * p] = v, lz[2 * p + 1] = v;
      return;
    }
    int m = (L + R) >> 1;
    update(i, v, 2 * p, L, m), update(i, v, 2 * p + 1, m + 1, R);
    val[p] = max(val[2 * p], val[2 * p + 1]);
  }
  int find(int v, int h = 0, int p = 1, int L = 0, int R = n) {
    if (L == R) return L;
    prop(p, L, R);
    int m = (L + R) >> 1;
    prop(2 * p, L, m), prop(2 * p + 1, m + 1, R);
    int tmp = max(h, val[2 * p]);
    if (v < tmp) return find(v, h, 2 * p, L, m);
    return find(v, tmp, 2 * p + 1, m + 1, R);
  }
} st;
int main() {
  memset(&st, 0, sizeof st);
  std::ios::sync_with_stdio(false);
  scanf("%d", &(n));
  int a;
  scanf("%d", &(a));
  st.update(n, a);
  static int ev[N];
  for (long long i = 2, __R = n; i <= __R; i++) {
    scanf("%d", &(a));
    int j = st.find(a);
    st.update(j, a);
    ev[0]++;
    ev[j + 1]--;
  }
  for (long long i = 1, __R = n; i <= __R; i++)
    if (ev[i] == 0) {
      printf("%d\n", i - 1);
      return 0;
    }
  return 0;
}
