#include <bits/stdc++.h>

using namespace std;

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldbl;
typedef pair<int, int> pii;
typedef pair<uint, uint> puu;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<uint> vu;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<pii> vpii;
typedef vector<puu> vpuu;
typedef vector<pll> vpll;
typedef vector<pull> vpull;
typedef vector<string> vstr;
typedef vector<double> vdbl;
typedef vector<ldbl> vldbl;
#define pb push_back
#define ppb pop_back
#define pfr push_front
#define ppfr pop_front
#define emp emplace
#define empb emplace_back
#define be begin
#define rbe rbegin
#define all(x) (x).be(), (x).end()
#define rall(x) (x).rbe(), (x).rend()
#define fir first
#define sec second
#define mkp make_pair
#define brif(cond) if (cond) break
#define ctif(cond) if (cond) continue
#define retif(cond) if (cond) return
void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);}
template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;}
template<typename T> T extgcd(T a, T b, T &x, T &y)
{
    T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
    while (b) {
        T q = a/b; a %= b; swap(a, b);
        x0 -= q*x1; swap(x0, x1);
        y0 -= q*y1; swap(y0, y1);
    }
    x = x0; y = y0; return a;
}
int ctz(uint x) {return __builtin_ctz(x);}
int ctzll(ull x) {return __builtin_ctzll(x);}
int clz(uint x) {return __builtin_clz(x);}
int clzll(ull x) {return __builtin_clzll(x);}
int popcnt(uint x) {return __builtin_popcount(x);}
int popcntll(ull x) {return __builtin_popcountll(x);}
int bsr(uint x) {return 31^clz(x);}
int bsrll(ull x) {return 63^clzll(x);}

#define N 200016

int p[N], inv[N];
int q[N]; /// indices
pii iv[N]; /// val = [l', r') idx = r

void dump(int b, int e)
{
    cerr << "  st =";
    for (int j = b; j < e; ++j) cerr << ' ' << q[j];
    cerr << '\n';
}

int main()
{
    canhazfast();

    int n, k, ans;
    int b, e, l, r;

    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        inv[p[i]] = i;
    }

    /// left:
    //cerr << "left:\n";
    b = e = 0;
    l = 0;
    for (int i = 0; i < k; ++i) {
        //cerr << "i = " << i << '\n';
        /// push back p[i]
        while (b < e && p[q[e-1]] > p[i]) {
            if (q[--e]+1 == l) --l; //dump(b, e);
        }
        q[e++] = i; //dump(b, e);
        if (i == l) ++l;
        //cerr << "  l = " << l << '\n';
    }
    iv[k].fir = l;
    for (int i = k; i < n; ++i) {
        //cerr << "i = " << i << '\n';
        /// cur iv = [i+1-k, i+1)
        /// push back p[i]
        while (b < e && p[q[e-1]] > p[i]) {
            if (q[--e]+1 == l) --l; //dump(b, e);
        }
        q[e++] = i; //dump(b, e);
        if (i == l) ++l;
        /// pop front p[i-k]
        if (q[b] == i-k) ++b;//, dump(b, e);
        /// fixup
        if (l <= i-k) {
            ++l;
            for (int j = b; j < e && q[j] == l; ++j) l = q[j]+1;
        }
        //cerr << "  l = " << l << '\n';
        iv[i+1].fir = l;
    }

    /// right:
    //cerr << "right:\n";
    b = e = 0;
    r = n;
    for (int i = n-1; i >= n-k; --i) {
        //cerr << "i = " << i << '\n';
        /// push back p[i]
        while (b < e && p[q[e-1]] < p[i]) {
            if (q[--e] == r) ++r; //dump(b, e);
        }
        q[e++] = i; //dump(b, e);
        if (i == r-1) --r;
        //cerr << "  r = " << r << '\n';
    }
    iv[n].sec = r;
    for (int i = n-k-1; i >= 0; --i) {
        //cerr << "i = " << i << '\n';
        /// cur iv = [i, i+k)
        /// push back p[i]
        while (b < e && p[q[e-1]] < p[i]) {
            if (q[--e] == r) ++r; //dump(b, e);
        }
        q[e++] = i; //dump(b, e);
        if (i == r-1) --r;
        /// pop front p[i+k]
        if (q[b] == i+k) ++b;//, dump(b, e);
        /// fixup
        if (r > i+k) {
            --r;
            for (int j = b; j < e && q[j]+1 == r; ++j) r = q[j];
        }
        //cerr << "  r = " << r << '\n';
        iv[i+k].sec = r;
    }
    /// dbg
    /*for (int i = k; i <= n; ++i) {
        cerr << "[" << i-k << ", " << i << ") -> [" << iv[i].fir << ", " << iv[i].sec << ")\n";
    }*/

    for (int i = k; i <= n; ++i) {
        if (iv[i].fir >= iv[i].sec) iv[i].fir = iv[i].sec = 0;
    }
    sort(iv+k, iv+n+1);
    ans = unique(iv+k, iv+n+1)-iv-k;
    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 4224