题目链接:http://codeforces.com/gym/101755/problem/K
#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll unsigned long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
const int maxn =2e5+5;
const int mod=9999991;
const int ub=1e6;
const double e=2.71828;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}/*
题意要读对,选的人是可以从
序列的人里面选的,所以贪心+二分就行了。注意最后要和m取最小值。*/int n,m,a[maxn];bool check(int x)
{int init=x,ret=0;for(int i=1;i<=n;i++){if(a[i]<=ret){ret++;}else{if(init){init--;ret++;}}}return ret+init>=m;
}
int main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) scanf("%d",&a[i]);int l=0,r=n,ans=m;while(l<=r){int mid=l+r>>1;if(check(mid)) {ans=mid;r=mid-1;}else l=mid+1;}printf("%d\n",min(m,ans));return 0;
}