using System; namespace JengaSort { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int[] a = new int[N]; int[] ai = new int[N+1]; bool[] moved = new bool[N+1]; int[] max = new int[N]; string[] vals = Console.ReadLine().Split(' '); for(int i=0; i < N; i++) { a[i] = int.Parse(vals[i]); ai[a[i]] = i; } int m = 0; for (int i = 0; i < N; i++) { if (m < a[i]) m = a[i]; max[i] = m; } int count = 0; for(int n= N-1; n >= 1; n--) { if(moved[n+1]) { moved[n] = true; count++; } else { if(ai[n] > 0) { if(max[ai[n] - 1] > n) { moved[n] = true; count++; } } } } Console.WriteLine(count); } } }