#include<iostream>
using namespace std;
int main()
{
    int n, middle = 0, result, streak = 0, adding = 0;
    cin>>n;
    int h[n];
    for(int i = 0; i < n; i++)
    {
        cin>>h[i];
    }
    for(int i = 1; i < n; i++)
    {
        if(((h[i-1] < h[i] && h[i+1] > h[i]) || (h[i-1] > h[i] && h[i+1] < h[i])) && (i != n-1))
        {
            streak++;
        }
        else
        {
            for(int c = 0; streak-c >= 1; c++)
            {
                adding += streak-c;
            }
            streak = 0;
        }
        middle += adding;
        adding = 0;
    }
    result = (n-1+middle)*2;
    cout<<result;
    return 0;
}
