Олимпиадный тренинг

Задача 43532. Books off the shelf


Peter has an extensive library of books. Now he stands near the shelf with adventure stories. It contains n books. All books on Petya's shelf are always numbered from left to right. Book number i has ai pages. On the shelf, next to which Petya is now standing, the number of pages in each book is different.

The peculiarity of the shelves in Petya's library is such that he can only take the last book from the shelf (that is, either the leftmost or the rightmost).

Petya is leaving for the holidays with his grandmother and wants to take two books with him, the thickest and the thinnest. Help Petya find out what is the minimum number of books he needs to remove from the shelf before he gets the two books he needs. Please note that Petya also removes the thickest and thinnest books from the shelf, so they are also considered. 

 

Input
The first line contains a single integer n (2 <= n <= 100) - the number of books on the shelf.  The second line contains n different integers a1, a2, ..., an (1 <= ai  <= 106) - number of pages in the book.

 

Imprint
Print a single integer — the minimum number of books Petya needs to remove from the shelf.

 
Examples
# Input Output
1
5
1 5 4 3 2
2
2
8
2 1 3 4 5 6 8 7
4

Запрещенные операторы:max;min