#include <stdio.h>
#define DIM 200011

int V[DIM];
int W[DIM];
int N, T, a, b, i, D, p, u, m;

int trece(int x) {
	int i, aux, M = 1;
	W[1] = V[1];
	
	if (W[1] > 0 && W[1] < x) {
		W[1] = -W[1];
	}
	if (W[1]<0 && -W[1] > D)
		return 0;
	for (i=2;i<=N;i++) {
		aux = V[i];
		if (aux > 0 && aux < x) {
			aux = -aux;
		}
		if (aux > 0)
			W[++M] = aux;
		else
			if (W[M] > 0)
				W[++M] = aux;
			else
				W[M] += aux;
		if (W[M]<0 && -W[M] > D)
			return 0;
	}
	return 1;
}

int main() {
	FILE *f = fopen("obstacole.in","r");
	FILE *g = fopen("obstacole.out","w");
	fscanf(f,"%d %d",&T,&D);
	fscanf(f,"%d",&a);
	if (a == 0)
		V[++N] = 1;
	else
		V[++N] = -1;
	for (i=1;i<T;i++) {
		fscanf(f,"%d",&b);
		if (b==a)
			if (b == 0)
				V[N]++;
			else
				V[N]--;
		else {
			if (b == 0)
				V[++N] = 1;
			else
				V[++N] = -1;
		}
		a = b;
	}
	fclose(f);
	
	p = 1;
	u = T;
	while (p<=u) {
		m = (p+u)/2;
		if (trece(m))
			p = m+1;
		else
			u = m-1;
	}
	
	fprintf(g,"%d\n",u);
	fclose(g);
/*
	for (i=1;i<=N;i++)
		printf("%d ",V[i]);
*/
	return 0;
}
