using System; using System.Linq; using System.Collections.Generic; namespace GengaSort { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); LinkedList An = new LinkedList(Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray()); if (N > An.Count) N = An.Count; int count = 0, max; LinkedListNode Ax, Xx; do { Xx = null; max = 0; Ax = An.First; while (Ax != null) { if (Ax.Value > max) max = Ax.Value; if (max > Ax.Value && (Xx == null || Xx.Value < Ax.Value)) Xx = Ax; Ax = Ax.Next; } if (Xx != null) { count++; An.AddFirst(Xx.Value); An.Remove(Xx); } } while (Xx != null); Console.WriteLine(count); } } }