#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template<size_t I,class H,class...T>struct TupleReaderWriter{static tuple<H,T...> r(istream&i){H v;i>>v;return tuple_cat(tuple<H>(v),TupleReaderWriter<sizeof...(T)-1,T...>::r(i));}static void w(ostream&o,tuple<H,T...>&t,string d){TupleReaderWriter<I-1,H,T...>::w(o,t,d);o<<d<<get<I>(t);}};
template<class H,class...T>struct TupleReaderWriter<0,H,T...>{static tuple<H,T...> r(istream&i){H v;i>>v;return tuple<H>(v);}static void w(ostream&o,tuple<H,T...>&t,string d){o<<get<0>(t);}};
template<class...T>istream&operator>>(istream&i,tuple<T...>&t){t=TupleReaderWriter<sizeof...(T),T...>::r(i);return i;}
template<class...T>ostream&operator<<(ostream&o,tuple<T...>&t){string delim=" ";TupleReaderWriter<sizeof...(T)-1,T...>::w(o,t,delim);return o;}
template<class T>istream&operator>>(istream&i,vector<T>&v){for(auto&x:v)i>>x;return i;}
template<class T>ostream&operator<<(ostream&o,vector<T>&v){string delim="";for(auto&x:v)o<<delim<<x,delim=" ";return o;}
template<class T>using rev_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T>using vector2d=vector<vector<T>>;
struct fixprec{int x;fixprec(int d):x(d){}};
ostream&operator<<(ostream&o,fixprec f){return o<<fixed<<setprecision(f.x);}
void R_YESNO(bool p) { cout << (p ? "YES" : "NO") << endl; }
void R_YesNo(bool p) { cout << (p ? "Yes" : "No") << endl; }
/*!* [union_find] *!*/
/*+* UnionFind 木 *+*/
// T (typename) : グラフの頂点を表す型, (int)parent を持つ必要あり
template <typename T>
struct UnionFind {
    vector<T> &u;
    UnionFind(vector<T> &u) : u(u) {
        for (int i = 0; i < u.size(); i++) {
            u[i].parent = i;
        }
    }
    int root(int x) {
        if (x < 0 || u.size() <= x) return -1;
        else if (x == u[x].parent) return x;
        else return u[x].parent = root(u[x].parent);
    }
    bool connect(int a, int b) {
        a = root(a), b = root(b);
        if (a == b || a < 0 || b < 0) return false;
        { /* do something */ }
        u[b].parent = a;
        return true;
    }
};

struct Node {
    int parent;
};

int main() {
    int N, K;
    cin >> N >> K;
    vector<int> P(N), inversion(N, 0);
    vector<Node> v_uf(N - K + 1);
    UnionFind<Node> uf(v_uf);
    for(int &x : P) cin >> x;
    int cnt = 0;
    // ソート済み区間の番号
    int last_seg = -1;
    for (int i = 0; i < N; i++) {
        int j = i - (K - 1);
        if (j >= 0) {
            if (cnt == 0) {
                if (last_seg >= 0) uf.connect(last_seg, j);
                last_seg = j;
            }
            cnt -= inversion[j];
        }
        if (P[i] > P[i + 1]) inversion[i] = 1;
        cnt += inversion[i];
    }
    set<int> s;
    for(int j = 1; j < N; j++) {
        if(s.size() >= K - 1) {
            //       K
            //  <--------->
            // i..........j
            // |---------|
            //  |---------|
            int i = j - K;
            if(P[i] < *s.begin() && *s.rbegin() < P[j]) {
                uf.connect(i, i + 1);
            }
            s.erase(P[i + 1]);
        }
        s.insert(P[j]);
    }
    set<int> ans;
    for(Node x : uf.u) {
        ans.insert(x.parent);
    }
    cout << ans.size() << endl;
    return 0;
}



Measured memory_bytes: 12032