#include <bits/stdc++.h>
using namespace std;
#define int long long 
int k,n;
int a[3000010];
int ans;
int s1[3000010],s2[3000010],t1,t2,h1=1,h2=1;
signed main()
{

	scanf("%d%d",&k,&n);
	for(int i=1;i<=n;++i)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;++i)
	{
		bool flag=1;
		while(t1>=h1&&a[s1[t1]]<=a[i])
			t1--;	
		s1[++t1]=i;
		while(t2>=h2&&a[s2[t2]]>=a[i])
			t2--;
		s2[++t2]=i;
		while(a[s1[h1]]-a[s2[h2]]>k)
		{
			if(h1<t1&&s1[h1]<s2[h2])
				h1++;
			else if(h2<=t2)
				h2++;
			else{
				flag=0;
				break;
			}
		}
		if(flag)
			ans=max(ans,i-max(s1[h1-1],s2[h2-1]));
	}
	printf("%d\n",ans);
	return 0;
}