#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-5;
const ld pi = acos(-1.0);

struct uf {
private:
	vector<int> par, ran;
public:
	uf(int n) {
		par.resize(n, 0);
		ran.resize(n, 0);
		rep(i, n) {
			par[i] = i;
		}
	}
	int find(int x) {
		if (par[x] == x)return x;
		else return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y)return;
		if (ran[x] < ran[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (ran[x] == ran[y])ran[x]++;
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};

struct SegT {
private:
	int sz; vector<int> node;
	const int init_c = -mod;
public:
	SegT(vector<int> v) {
		int n = v.size();
		sz = 1;
		while (sz < n)sz *= 2;
		node.resize(2 * sz - 1, init_c);
		rep(i, n) {
			node[i + sz - 1] = v[i];
		}
		per(i, sz - 1) {
			node[i] = f(node[2 * i + 1], node[2 * i + 2]);
		}
	}
	int f(int a, int b) {
		return max(a, b);
	}
	void update(int k, int a) {
		k += sz - 1;
		node[k] = a;
		while (k > 0) {
			k = (k - 1) / 2;
			node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
		}
	}
	int query(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0)r = sz;
		if (r <= a || b <= l)return init_c;
		else if (a <= l && r <= b)return node[k];
		else {
			int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
			int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
			return f(vl, vr);
		}
	}
};

struct SegT2 {
private:
	int sz; vector<int> node;
	const int init_c = mod;
public:
	SegT2(vector<int> v) {
		int n = v.size();
		sz = 1;
		while (sz < n)sz *= 2;
		node.resize(2 * sz - 1, init_c);
		rep(i, n) {
			node[i + sz - 1] = v[i];
		}
		per(i, sz - 1) {
			node[i] = f(node[2 * i + 1], node[2 * i + 2]);
		}
	}
	int f(int a, int b) {
		return min(a, b);
	}
	void update(int k, int a) {
		k += sz - 1;
		node[k] = a;
		while (k > 0) {
			k = (k - 1) / 2;
			node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
		}
	}
	int query(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0)r = sz;
		if (r <= a || b <= l)return init_c;
		else if (a <= l && r <= b)return node[k];
		else {
			int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
			int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
			return f(vl, vr);
		}
	}
};


void solve() {
	int n, k; cin >> n >> k;
	vector<int> p(n);
	rep(i, n) {
		cin >> p[i];
	}
	vector<int> ri(n);
	stack<int> st;
	rep(i, n) {
		if(i>0&&p[i-1]>p[i]){
			while (!st.empty()) {
				int id = st.top(); st.pop();
				ri[id] = i;
			}
		}
		st.push(i);
	}
	while (!st.empty()) {
		int id = st.top(); st.pop();
		ri[id] = n;
	}
	//cout << "hello" << endl;
	vector<int> sotd;
	vector<int> b(n,0);
	vector<int> rb(n + 1, 0);
	rep(i, n-k+1) {
		if (ri[i] >= i + k) {
			b[i] = 1; sotd.push_back(i);
		}
		rb[i + 1] = rb[i] + b[i];
	}
	uf u(n-k+2);
	rep(i, (int)sotd.size() - 1) {
		u.unite(sotd[i], sotd[i + 1]);
	}
	int ans = 0;

	SegT ma(p); SegT2 mi(p);
	rep(i, n - k + 1) {
		if (ri[i] >= i + k) {
			int sum = rb[n - k + 1] - rb[i + k];
			//ans += sum;
		}
	}
	rep(i, n-k) {
		int l = 0, r = k;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			int loc = i + mid;
			bool f = true;
			if (i + k + mid > n)f = false;
			if (ri[i] < loc) {
				f = false;
			}
			if (ri[i + k] < i + k +mid) {
				f = false;
			}
			if (!f) {
				r = mid; continue;
			}
			int mal = ma.query(i, i + mid);
			int mimi = mi.query(i + mid, i + k);
			if (mal > mimi) {
				f = false;
			}
			int mami = ma.query(i + mid, i + k);
			int mir = mi.query(i + k, i + k + mid);
			if (mami > mir) {
				f = false;
			}
			if (!f) {
				r = mid;
			}
			else {
				l = mid;
			}

		}
		rep(j, l) {
			u.unite(i, i + 1);
			i++;
		}
		//ans += l;
	}
	int g = n - k + 1;
	rep(i, n - k + 1) {
		if (!u.same(i, g)) {
			ans++; u.unite(i, g);
		}
	}
	cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	//stop
	return 0;
}


Measured memory_bytes: 11140