AlgoUniversity
  • Go Back
Discussion
Help DJ :

Author

Lokesh

Difficulty Level : Easy

Submissions : 104

Asked In : Cred

Marks :10

: 0 | : 0

DJ Varun works in a club and plays cool songs. He has $$$N$$$ disks of different albums (each of equal radius). Every disk has a distinct number out of $$$1$$$ to $$$N$$$ associated with it. Disks are placed one over the other in a single pile. Varun wants to sort this pile of disks in an increasing order i.e., top to bottom. But he has a very special method of doing this. In a single step, he can only choose one disk out of the pile and he can only put it at the top. So the task here is that Varun wants to sort his pile of disks in a minimum number of possible steps. What is the minimum number of possible steps to sort the pile so that Varun can check whether he is doing his work right or wrong?

Input

The first line contains integer N, the size of the array, followed by an array of size N containing integers 1 to N in any random order, which shows the position of disks from top to bottom.

$$$1 \le N \le 10^6$$$

Output

Print the Minimum number of steps needed to sort the pile. If it can't be sorted, then return the output as -1.

Examples

Input
5
5 4 3 2 1
Output
0
Input
5
1 2 3 4 5
Output
4
Input
5
1 5 3 4 2
Output
3

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.