#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);(i)++)
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);(i)++)
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);(i)--)
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);(i)--)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; }return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; }return 0; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& input, vector<T>& v) { for (auto& elemnt : v)input >> elemnt; return input; }

class maxtree {
    int n = 1; VI dat;
public:
    maxtree(VI a) {
        int _n = a.size();
        while (n < _n)n *= 2;
        dat.resize(2 * n - 1, 0);
        FOR(i, 0, _n)dat[i + n - 1] = a[i];
        rFOR(i, 0, n - 1)dat[i] = max(dat[2 * i + 1], dat[2 * i + 2]);
    }
    int Max(int a, int b, int k, int l, int r) {
        if (b <= l || r <= a)return 0;
        if (a <= l && r <= b)return dat[k];
        else {
            int s = Max(a, b, 2 * k + 1, l, (l + r) / 2);
            int t = Max(a, b, 2 * k + 2, (l + r) / 2, r);
            return max(s, t);
        }
    }
    int Max(int a, int b) { return Max(a, b, 0, 0, n); }
    int Min(int a, int b, int k, int l, int r) {
        if (b <= l || r <= a)return 0;
        if (a <= l && r <= b)return dat[k];
        else {
            int s = Min(a, b, 2 * k + 1, l, (l + r) / 2);
            int t = Min(a, b, 2 * k + 2, (l + r) / 2, r);
            return min(s, t);
        }
    }
    int Min(int a, int b) { return Min(a, b, 0, 0, n); }
};

class mintree {
    int n = 1; VI dat;
public:
    mintree(VI a) {
        int _n = a.size();
        while (n < _n)n *= 2;
        dat.resize(2 * n - 1, INF);
        FOR(i, 0, _n)dat[i + n - 1] = a[i];
        rFOR(i, 0, n - 1)dat[i] = min(dat[2 * i + 1], dat[2 * i + 2]);
    }
    int Min(int a, int b, int k, int l, int r) {
        if (b <= l || r <= a)return INF;
        if (a <= l && r <= b)return dat[k];
        else {
            int s = Min(a, b, 2 * k + 1, l, (l + r) / 2);
            int t = Min(a, b, 2 * k + 2, (l + r) / 2, r);
            return min(s, t);
        }
    }
    int Min(int a, int b) { return Min(a, b, 0, 0, n); }
};

int main() {
    init();

    int n, k;
    cin >> n >> k;
    VI p(n);
    FOR(i, 0, n)cin >> p[i];

    maxtree max_seg(p);
    mintree min_seg(p);

    vector<bool> isSorted(n - k + 1);
    int cnt = 0, itr = 1;
    FOR(i, 0, n - k + 1) {
        while (itr < i + k) {
            if (p[itr - 1] > p[itr])cnt++;
            itr++;
        }
        isSorted[i] = (cnt == 0);
        if (p[i] > p[i + 1])cnt--;
    }

    vector<bool> New(n - k + 1, false);
    New[0] = true;
    FOR(i, 0, n - k) {
        int MAX = max_seg.Max(i, i + k);
        int MIN = min_seg.Min(i, i + k);
        New[i + 1] = !(p[i] == MIN && MAX < p[i + k]);
    }

    int ans = 0;
    bool plus = false;
    FOR(i, 0, n - k + 1) {
        if (isSorted[i])plus = true;
        else if (New[i]) {
            ans++;
        }
    }
    if (plus)ans++;

    cout << ans << "\n";
}

Measured memory_bytes: 5872