program lexicographic_constraints
  implicit none
  integer              :: n, m, i, j, k
  integer, allocatable :: a(:), s(:)
  read(*,*) n
  allocate(a(n))
  read(*,*) a(:)
  m = maxval(a)
  allocate(s(m))
  s = 0
  k = 1
  loop: do i = 2, n
     if (a(i) .gt. a(i-1)) cycle
     if (a(i) .ne. m) s(a(i)+1:m) = 0
     s(a(i)) = s(a(i)) + 1
     if (s(a(i)) .eq. k) then
        if (a(i) .eq. 1) then
           k = k + 1
           cycle
        else
           do j = 1, a(i)-1
              if (s(j) .ne. k-1) then
                 s(j) = s(j) + 1
                 s(j+1:a(i)) = 0
                 cycle loop
              end if
           end do
           k = k + 1
        end if
     end if
  end do loop
  write(*,'(i0)') k
  deallocate(a,s)
  stop
end program lexicographic_constraints