#include<iostream>
using namespace std;
int main()
{
    int N;
    cin>>N;
    int arr[100001];
    int counter=0;
    for(int i=0;i<N;i++)
    {
        cin>>arr[i];
    }
     for(int i=0;i<N;i++)
    {
        for(int j=i+1;j<N;j++)
        {
                if((arr[j-1]<arr[i] && arr[j-1]<arr[j])||(arr[j-1]>arr[i] && arr[j-1]>arr[j]))
                {
                //     cout<<arr[i]<<" "<<arr[j]<<" "<<arr[j-1]<<" ";
                    j=N;
                }
                else
                {
                    counter++;
                }
        }
    }
    cout<<counter*2; /// zala-hotel, hotel-zala
    return 0;
}
