#include <bits/stdc++.h>
using namespace std;
typedef long long ll;



int main() {
#ifdef HOME
    assert(freopen("in", "r", stdin));
#endif


    int n;
    while (scanf("%d", &n) == 1) {
        vector<int> a(n);
        vector<int> pos(n);
        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            --a[i];
            pos[a[i]] = i;
        }

        vector<int> frst(1, n - 1);
        for (int i = n - 2; i >= 0; i--) {
            bool nw = 0;
            if (frst.size() == 1) {
                nw = pos[i] > pos[frst.back()];
            } else {
                int p1 = pos[i];
                int p2 = pos[frst.back()];
                int p3 = pos[frst[(int)frst.size() - 2]];

                if ((p2 > p3) != (p1 > p3)) nw = p1 < p2;
                else nw = p1 > p2;
            }
            if (nw) frst.push_back(i);
            else frst.back()--;
        }
        printf("%d\n", (int)frst.size() - 1);
    }


#ifdef HOME
    cerr << "time: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
    return 0;
}