#include<bits/stdc++.h>
using namespace std;
long long q,z[1000005],n[1000005],nl,n1;
stack<int> sb;
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>q;
for(int i=1;i<=q;i++){
cin>>z[i];
while (!sb.empty()&&z[sb.top()]<z[i])
{
nl++;
sb.pop();
}
if(!sb.empty())
nl++;
sb.push(i);
}
cout<<nl;
}