// Arup Guha
// 1/28/2018
// Solution to Jan 2018 USACO Bronze Problem: Out of Place

import java.util.*;
import java.io.*;

public class outofplace_bronze {

	final public static int MAX = 1000000;

	public static void main(String[] args) throws Exception {

		// Read the data.
		Scanner stdin = new Scanner(new File("outofplace.in"));
		int n = stdin.nextInt();
		int[] arr = new int[n];
		for (int i=0; i<n; i++)
			arr[i] = stdin.nextInt();

		// Find the badIndex.
		int badIndex = -1;
		for (int i=1; i<n; i++) {
			if (arr[i] < arr[i-1]) {
				badIndex = i;
				break;
			}
		}

		HashSet<Integer> skip = new HashSet<Integer>();

		// Something is actually out of place...
		if (badIndex != -1) {

			// Means we have to shift the smaller number to the left.
			if (badIndex == n-1 || (badIndex < n-1 && arr[badIndex-1] <= arr[badIndex+1])) {

				int i = badIndex-1;

				// Keep on going left until the numbers aren't bigger than us.
				while (i >= 0 && arr[i] > arr[badIndex]) {
					skip.add(arr[i]);
					i--;
				}
			}

			// Means we have to move the big number to the right.
			else {

				int i = badIndex;

				// Keep going right until the numbers aren't smaller than us.
				while (i < n && arr[i] < arr[badIndex-1]) {
					skip.add(arr[i]);
					i++;
				}
			}
		}

		// Ta da!
		PrintWriter out = new PrintWriter(new FileWriter("outofplace.out"));
		out.println(skip.size());
		out.close();
		stdin.close();
	}
}