import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] nums = new int[n]; int[] pos = new int[n + 1]; for (int i = 0; i < n; i++) { nums[i] = sc.nextInt(); pos[nums[i]] = i; } //System.out.println(Arrays.toString(nums)); //System.out.println(Arrays.toString(pos)); System.out.println(solve(nums, pos)); sc.close(); } public static int solve (int[] nums, int[] pos) { int lim = nums.length; for (int i = 0; i < lim; i++) { if (isSorted(nums)) { return i; } else { int p = pos[i + 1]; nums[p] = -1; } } return -1; } public static boolean isSorted (int[] nums) { int head = -1; for (int i = 0; i < nums.length; i++) { if (nums[i] == -1) continue; if (head == -1) { head = nums[i]; continue; } else if (head > nums[i]) { return false; } else { head = nums[i]; } } return true; } }