#include <queue>
#include <math.h>
#include <stack>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <iomanip>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
int st[N],top,num[N],a[N];
int main()
{
int n;
cin >> n;
LL ans=0;
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
num[x]++;
while(top > 0 && st[top] <x)
{
ans+=num[st [top]];
num[st [top]]=0;
top--;
}
if(st[top]!=x)
{
if(st[top]!=x)
ans++;
st[++top]=x;
}
else
{
ans+=num[st[top]];
if(top==1)
ans--;
}
}
cout << ans << endl;
return 0;
}