#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int totalCases, testNum;
int N;
vector<int> A;
int LifeTime[maxn];
int Killer[maxn];
int pos[maxn];
void preprocess() {}
bool input() {
  scanf("%d", &N);
  A.push_back(0);
  pos[0] = 0;
  for (int i = 1; i <= N; i++) {
    int x;
    scanf("%d", &x);
    A.push_back(x);
    pos[x] = i;
  }
  return true;
}
void solve() {
  LifeTime[0] = N + 1;
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    int p = pos[i];
    if (A[p - 1] > i) {
      LifeTime[i] = 1;
      ans = max(ans, LifeTime[i]);
      Killer[i] = A[p - 1];
    } else {
      int prev = A[p - 1];
      int LT = LifeTime[prev];
      while (prev > 0 && prev < i) {
        LT = max(LT, LifeTime[prev]);
        prev = Killer[prev];
      }
      if (prev < i) {
        LifeTime[i] = N + 1;
        Killer[i] = prev;
        continue;
      } else {
        LifeTime[i] = LT + 1;
        Killer[i] = prev;
      }
      if (LifeTime[i] <= N) ans = max(ans, LifeTime[i]);
    }
  }
  cout << ans << endl;
}
int main() {
  preprocess();
  totalCases = 1;
  for (testNum = 1; testNum <= totalCases; testNum++) {
    if (!input()) break;
    solve();
  }
}
