import java.util.Scanner; import java.util.ArrayList; import java.util.InputMismatchException; public class JengaSort { static Scanner scanner = new Scanner(System.in); public static void main(String[] args){ int N = Input(1 , 100000); ArrayList High = new ArrayList<>(); for(int i = 0 ; i < N ; i++){ High.add(Input(1,N)); } int Result = Processing(N,High); if(Result == 1){ System.out.println(N - Result); }else{ System.out.println(N - Result - 1); } } public static int Input(int Min , int Max){ int Number = scanner.nextInt(); try{ if(Number < Min || Number > Max){ System.out.println(Min + "以上" + Max + "以下で入力してください"); System.exit(0); } }catch(InputMismatchException e){ System.out.println("数字を入力してください"); System.exit(0); }catch(Exception E){ System.out.println("予期せぬエラーです"); System.exit(0); } return Number; } public static int Processing(int N , ArrayList High){ int Count = 1; for(int i = N - 1 ; i > 0 ; i--){ if(High.get(i) == N){ for(int j = i - 1 ; j > 0 ; j--){ if(High.get(j) == N - 1){ Count++; N--; } } } } return Count; } }