#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fll
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
const int maxn=2e5+10;
const int maxt=(1<<20)+10;
int n,k,p[maxn];
int dat[maxt];
int resp[maxn],rf[maxn];
stack<int> ls[maxn];
stack<pii> cur;
int bg[maxn],ed[maxn];
void build(int x,int l,int r,int op)
{
	if(l==r)
	{
		resp[l]=x;
		dat[x]=op?-1:inf;
		return;
	}
	int mid=(l+r)>>1;
	build(x*2,l,mid,op);
	build(x*2+1,mid+1,r,op);
	if(op)dat[x]=max(dat[x*2],dat[x*2+1]);
	else dat[x]=min(dat[x*2],dat[x*2+1]);
}
void update(int x,int l,int r,int p,int v,int op)
{
	if(l==r)
	{
		dat[x]=v;
		return;
	}
	int mid=(l+r)>>1;
	if(p<=mid)update(x*2,l,mid,p,v,op);
	else update(x*2+1,mid+1,r,p,v,op);
	if(op)dat[x]=max(dat[x*2],dat[x*2+1]);
	else dat[x]=min(dat[x*2],dat[x*2+1]);
//	cout<<x<<" "<<l<<" "<<r<<" "<<dat[x]<<endl;
}
int query(int x,int l,int r,int tl,int tr,int op)
{
//	cout<<"q "<<x<<" "<<l<<" "<<r<<" "<<tl<<" "<<tr<<" "<<op<<endl;
	if(l>tr||r<tl)return op?-1:inf;
	if(tl<=l&&r<=tr)
	{
//		cout<<dat[x]<<endl;
		return dat[x];
	}
	int mid=(l+r)>>1;
	return op?max(query(x*2,l,mid,tl,tr,op),query(x*2+1,mid+1,r,tl,tr,op)):
			min(query(x*2,l,mid,tl,tr,op),query(x*2+1,mid+1,r,tl,tr,op));
}
set<pii > st;
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++)
		scanf("%d",&p[i]);
	while(!cur.empty())cur.pop();
	for(int i=n-1;i>=0;i--)
	{
		while(!cur.empty()&&cur.top().first>p[i])cur.pop();
		if(!cur.empty())
		{
			rf[i]=cur.top().second;
			ls[rf[i]].push(i);
		}
		else rf[i]=-1;
		cur.push(make_pair(p[i],i));
	//	cout<<i<<" "<<rf[i]<<endl;
	}
	build(1,0,n-1,0);
	for(int i=0;i<k-1;i++)
	{
		if(rf[i]==-1)continue;
		if(i==ls[rf[i]].top())
		{
			update(1,0,n-1,rf[i],i,0);
	//		cout<<"upd "<<rf[i]<<" "<<i<<endl;
		}
	}
	for(int i=k-1;i<n;i++)
	{
		//ins
		if(rf[i]!=-1&&i==ls[rf[i]].top())
		{
			update(1,0,n-1,rf[i],i,0);
	//		cout<<"upd "<<rf[i]<<" "<<i<<endl;
		}
		//get
		bg[i]=query(1,0,n-1,i-k+1,i,0);
	//	cout<<bg[i]<<endl;
		//del
		if(rf[i-k+1]!=-1&&i-k+1==ls[rf[i-k+1]].top())
		{
			ls[rf[i-k+1]].pop();
			update(1,0,n-1,rf[i-k+1],ls[rf[i-k+1]].empty()?inf:ls[rf[i-k+1]].top(),0);
	//		cout<<"upd "<<" "<<rf[i-k+1]<<" "<<(ls[rf[i-k+1]].empty()?inf:ls[rf[i-k+1]].top(),0)<<endl;
		}
	}
	
	for(int i=0;i<n;i++)
		if(!ls[i].empty())ls[i].pop();
	while(!cur.empty())cur.pop();
	for(int i=0;i<n;i++)
	{
		while(!cur.empty()&&cur.top().first<p[i])cur.pop();
		if(!cur.empty())
		{
			rf[i]=cur.top().second;
			ls[rf[i]].push(i);
		}
		else rf[i]=-1;
		cur.push(make_pair(p[i],i));
	//	cout<<i<<" "<<rf[i]<<endl;
	}
	build(1,0,n-1,1);
	for(int i=n-1;i>n-k;i--)
	{
		if(rf[i]==-1)continue;
		if(i==ls[rf[i]].top())
		{
			update(1,0,n-1,rf[i],i,1);
	//		cout<<"upd "<<rf[i]<<" "<<i<<endl;
		}
	}
	for(int i=n-k;i>=0;i--)
	{
		//ins
		if(rf[i]!=-1&&i==ls[rf[i]].top())
		{
			update(1,0,n-1,rf[i],i,1);
	//		cout<<"upd "<<rf[i]<<" "<<i<<endl;
		}
		//get
		ed[i+k-1]=query(1,0,n-1,i,i+k-1,1);
	//	cout<<ed[i+k-1]<<endl;
		//del
		if(rf[i+k-1]!=-1&&i+k-1==ls[rf[i+k-1]].top())
		{
			ls[rf[i+k-1]].pop();
			update(1,0,n-1,rf[i+k-1],ls[rf[i+k-1]].empty()?-1:ls[rf[i+k-1]].top(),1);
	//		cout<<"upd "<<" "<<rf[i+k-1]<<" "<<(ls[rf[i+k-1]].empty()?-1:ls[rf[i+k-1]].top())<<endl;
		}
	}
	
	for(int i=k-1;i<n;i++)
		st.insert(make_pair(bg[i],ed[i]));
	printf("%d\n",(int)st.size());
	return 0;
}

Measured memory_bytes: 151680