#include<iostream>
#include<stack>
using namespace std;
stack<int> s;
int x,k,n,ans;
int main(){
cin>>n;
cin>>x;
s.push(x);
k=x;
for(int i=2;i<=n;i++){
cin>>x;
if(x<k){
s.push(x);
k=x;
}
else if(x==k){
ans++;
continue;
}
else{
while(x>s.top()){
s.pop();
if(s.empty()){
s.push(x);
k=x;
goto akioi;
}
}
s.push(x);
k=x;
}
akioi:
ans+=(s.size()-1);
}
cout<<ans<<endl;
return 0;
}