#CCBCHBAHAI0000164. Tướng quân đến duyệt đội (Arrival of the General)

Tướng quân đến duyệt đội (Arrival of the General)

Arrival of the General

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem

nn soldiers stand in a line with heights a1,…,ana_1,\ldots,a_n. The line is accepted when a maximum-height soldier is first and a minimum-height soldier is last. In one second, two adjacent soldiers may swap.

Find the minimum number of adjacent swaps required. With ties, moving the leftmost maximum to the front and the rightmost minimum to the back is optimal.

Input

  • Line 1: integer nn.
  • Line 2: nn heights.

Output

Print the minimum number of seconds.

Subtask

Subtask 1 (100 points): 2≤n≤1002\le n\le100; 1≤ai≤1001\le a_i\le100.

Example

Input

4
33 44 11 22

Output

2

Explanation

Move 4444 one position left and 1111 one position right: two adjacent swaps in total.