program mian
  implicit none
  integer :: N
  integer,allocatable :: A(:)
  integer :: mloc_tmp(1)
  integer :: maval
  integer :: maloc,miloc
  integer :: is,ie
  integer :: i,j
  read(*,*) N
  allocate(A(N))
  do i = 1,N
     read(*,*) A(i)
  end do
  i = 0
  do
     mloc_tmp = maxloc(A)
     maval= maxval(A)
     maloc = mloc_tmp(1)
     is = 1
     ie = maloc
     if (ie == 1 ) then
        call rma(A,1,1)
        i = i+ 1
        goto 999
        cycle
     end if
     do j = maloc-1,1,-1
        if (A(j) >= A(j+1)) then
           is = j+1
           exit
        end if
     end do
     call rma(A,is,ie)
     i = i+ 1
999     if (size(A) == 0) exit
  end do
  write(*,'(i0)') i
  stop
contains
  
  subroutine rma(A,is,ie)
    integer,intent(inout),allocatable :: A(:)
    integer :: is,ie
    integer :: N,M
    integer,allocatable :: B(:)
    M=size(A)
    allocate(B(M))
    B=A
    deallocate(A)
    N=M-(ie-is+1)
    allocate( A(N) )
    if (is-1>0) then
       A(1:is-1) = B(1:is-1)
       A(is:N) = B(ie+1:M)
    else 
       A(is+1:N) = B(ie+1:M)
    end if
    deallocate(B)
    return
  end subroutine rma
end program mian
