SOLVE

LATER

Infinity Permutations

/

Everyone is aware of the \(6\) infinity stones that exist in Marvel Cinematic Universe (MCU) and are in the possession of mighty Thanos. There is one problem though the stones are arranged randomly on his giant hand and he needs them to be arranged in increasing order of their respective powers so that he could use them according to the demand of the situation.

Seems quite simple, isnt it? But this isn't MCU, this is our CPU (Competitive Programming Universe), the situation here is more complicated:

1. There are \(N\) infinity stones on CPU with powers ranging from \(1\) to \(N\). You will be given a permutation \((A_1, A_2, A_3,...A_N)\) of these \(N\) distinct integers.

2. For arranging the elements you can perform following operation -

**Pick an element**of the sequence and**move**it to the**beginning**or**ending**of the sequence.

Thanos has less than \(12\) days left to prepare for the big fight, but you have only 5 hours to find the minimum number of operations required to get the job done.

**INPUT FORMAT:**

First line of input contains a single integer \(N\) denoting the no. of infinity stones.

Next \(N\) lines contain a single integer each denoting the power of \(i^{th}\) stone.

**OUTPUT FORMAT:**

Print the **minimum** number of operations required.

**CONSTRAINTS:**

\(1 \leq N ≤ 2×10^{5} \\ A_1, A_2, A_3,...A_N\ is\ a\ permutation\ of\ (1, 2,...N)\)

Explanation

For example, the sequence can be sorted in ascending order as follows:

- Move 2 to the beginning. The sequence is now (2,1,3,4)
- Move 1 to the beginning. The sequence is now (1,2,3,4)

Time Limit:
1.0 sec(s)
for each input file.

Memory Limit:
256 MB

Source Limit:
1024 KB

Initializing Code Editor...