This page is still under construction.

Parts of this page are still being built. What you see may change.

Kodkraft

Time limit2sMemory limit1024 MB

Summary
Given a yearly contest schedule over K divisions, find the shortest window containing each division 1 through K in order, counting only contests that occur between his first and final win.
Level

Medium6 of 10

Topics
Sliding window, Two pointers, Implementation
Solved
No attempts yet

Problem

Nicolas wants to start competing in programming on the site kodkraft\texttrademark. There are many different divisions to compete in, but since Nicolas is a new participant on kodkraft™, he has to start in the lowest division (division 1). Nicolas's goal is to reach the highest division (division KK) as quickly as possible and win a contest in it.

According to kodkraft™'s rules, you can only move up one division per contest, so he will have to compete in at least one contest in each division. However, Nicolas is very confident and therefore believes he will need to compete in exactly one contest in each division to move up to the next division. When a contest is held on kodkraft™, only one division competes at a time, and two contests never overlap in time. The contests also follow the same schedule every year.

Nicolas may begin his competing on kodkraft™ on whichever date of the year he wants. What Nicolas means by as quickly as possible is that as few contests as possible should take place on kodkraft™ (whether he participates in them or not) between the first contest he participates in and his first win in the highest division. Help Nicolas compute how many contests are required!

Input

The first line contains two integers NN and KK (1≤K≤N≤1061 \leq K \leq N \leq 10^6), the number of contests per year and the number of divisions.

This is followed by a line with NN integers x1,…,xNx_1, \dots, x_N (1≤xi≤K1 \leq x_i \leq K), the schedule of contests during one year. xix_i is the division that competes in the ii-th contest after New Year's. Every division between 11 and KK has at least one contest during the year.

Output

One integer, the smallest number of contests that need to take place on kodkraft™ from when he starts competing there until he has won division KK.

Examples3

  1. Example 1

    Input
    3 3
    3 2 1
    
    Expected output
    5
    
  2. Example 2

    Input
    3 2
    1 1 2
    
    Expected output
    2
    
  3. Example 3

    Input
    7 5
    2 1 1 4 3 2 5
    
    Expected output
    19