This page is still under construction.

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

Sequence Transformation

Interview

Time limit2sMemory limit512 MB

Summary
Given a sequence of non-negative integers, find the minimum number of single increments needed so that 1,2,...,h appear consecutively as a block, or report that it is impossible.
Level

Medium6 of 10

Topics
Array, Sliding window, Prefix sum, Greedy
Solved
No attempts yet

Problem

The math teacher dislikes Kolya very much and always makes him answer at the blackboard with the hardest problems.

Today she wrote a sequence of nn non-negative integers a1,a2,…,ana_1, a_2, \ldots, a_n on the blackboard and called Kolya up. In one action, the teacher allows Kolya to erase any number and write in its place a number one greater. The teacher demands that Kolya, in the minimum number of actions, make the numbers 1 through hh appear consecutively somewhere in this sequence.

Help Kolya find the minimum number of actions needed so that for some ii, ai=1a_i=1, ai+1=2a_{i+1}=2, ..., ai+h−1=ha_{i+h-1}=h, or determine that this is impossible and the teacher is again bullying poor Kolya with impunity.

Input

The first line of the input file contains two positive integers: nn and hh (1≤h≤n≤200 0001 \le h \le n \le 200\,000). The second line contains nn numbers aia_i, the initial values of the sequence (0≤ai≤n0 \le a_{i} \le n).

Output

In a single line of the output file, print the minimum number of actions with which Kolya can complete the task, or −1-1 if it is impossible.

Hint

In the first example, Kolya must increase the third number by 1 twice and the fourth number once. Then the sequence becomes 1, 1, 2, 3, and for i=2i=2 the required condition holds.

In the second example, it is impossible to obtain 1 and 2 consecutively in the sequence.

Examples2

  1. Example 1

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

    Input
    3 2
    1 3 2
    
    Expected output
    -1