Universal Cup Judging System

Universal Cup

Süre Sınırı: 1.0 s Bellek Sınırı: 1024 MB Toplam puan: 100
İstatistikler

Bartek is a well-known techno music producer, and he is currently perfecting his next hit. He has created a track consisting of $n$ beats, numbered consecutively from $1$ to $n$. After spending hours in a music editing program, he was about to export the mp3 file, but he noticed that the beats are currently arranged in the order $a_1, a_2, \dots, a_n$. Bartek needs to obtain an increasing order of beats, that is, $1, 2, \dots, n$.

The program allows two types of operations:

  • Move the last beat to any position, e.g. $(2, 4, 1, 5, \underline{3}) \to (2, \underline{3}, 4, 1, 5)$.
  • Move any beat to the beginning, e.g. $(2, 4, 1, \underline{5}, 3) \to (\underline{5}, 2, 4, 1, 3)$.

What is the minimum number of operations needed to arrange the beats in increasing order?

Input

The first line contains an integer $n$ ($1 \le n \le 200\,000$), denoting the number of beats.

The second line contains $n$ pairwise distinct integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n; a_i \ne a_j$), representing the current order of beats in the program.

Output

Output a single integer – the minimum number of operations required.

Examples

Input 1

6
3 2 5 4 1 6

Output 1

4

Input 2

9
3 1 2 5 6 4 9 7 8

Output 2

6

Note

We start with the sequence of beats $(3, 2, 5, 4, 1, 6)$. Here is one way to achieve the desired order in four operations:

  • Move the last beat two places to the left: $(3, 2, 5, \underline{6}, 4, 1)$.
  • Move beat $2$ to the beginning: $(\underline{2}, 3, 5, 6, 4, 1)$.
  • Move beat $1$ to the beginning: $(\underline{1}, 2, 3, 5, 6, 4)$.
  • Move the last beat two places to the left: $(1, 2, 3, \underline{4}, 5, 6)$.

It is possible to obtain an increasing order in other ways using four operations, but it cannot be done in three operations, so the result is $4$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.