Help required in this tricky array problem

Given an array A(A is a permutation for 1 to |A|) i.e contains all the numbers from 1 to |A| exactly once and |A| <=1e5, you can perform the following operation any number of times.

Move the first element X places to right, 0<=X<N, cost of this operation is X (distance moved)

Find the minimum cost to sort the array

Sample Input 1
1 2 3
Sample Output 1
0

Sample Input 2
1 3 2
Sample Output 2
3

Remember that we have to move only the first element every time.