def jenga_sort(A): d = {} for i in reversed(range(1, N)): for j in range(i): if A[i] < A[j]: d[A[i]] = i break if len(d) == 0: return 0 A.insert(0, A.pop(d[max(d)])) return jenga_sort(A) + 1 N = int(input()) A = [int(i) for i in input().split()] print(jenga_sort(A))