// Arup Guha
// 3/6/2021
// Solution to 2020 SER Problem: Dominating Duos

import java.util.*;
import java.io.*;

public class duos {

	public static void main(String[] args) throws Exception {
	
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(stdin.readLine());
		int[] vals = new int[n];
		for (int i=0; i<n; i++)
			vals[i] = Integer.parseInt(stdin.readLine())-1;
			
		// Store locations of each item.
		int[] loc = new int[n];
		for (int i=0; i<n; i++)
			loc[vals[i]] = i;
		
		// Here is a list of the indexes we already have.
		TreeSet<Integer> ts = new TreeSet<Integer>();
		ts.add(loc[n-1]);
		
		// Store result here.
		int res = 0;
		
		// Put each item in order from largest to smallest.
		for (int i=n-2; i>=0; i--) {
		
			Integer up = ts.higher(loc[i]);
			if (up != null) res++;
		
			Integer down = ts.lower(loc[i]);
			if (down != null) res++;
			
			// Now add this.
			ts.add(loc[i]);
		}
		
		// Ta da!
		System.out.println(res);
	}
}