/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>

#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>

#include <cmath>
#include <numeric>
#include <random>
#include <cstring>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <stack>

// type
#include <cstdint>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type ---------- */
using ll = long long;
#define int ll
#define P pair<ll, ll>

/* ---------- Constants  */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;

struct RangeMin {
private:
    int n;
    vector<int> node;

public:
    RangeMin(vector<int> v) {
        int sz = v.size();
        n = 1;
        while (n < sz) n *= 2;
        node.resize(2 * n - 1, INF);
        for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
        for (int i = n - 2; i >= 0; i--) node[i] = min(node[2 * i + 1], node[2 * i + 2]);
    }

    void update(int x, int val) {
        x += (n - 1);
        node[x] = val;
        while (x > 0) {
            x = (x - 1) / 2;
            node[x] = min(node[2 * x + 1], node[2 * x + 2]);
        }
    }

    int getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return INF;
        if (a <= l && r <= b) return node[k];

        int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
        int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
        return min(vl, vr);
    }
};

struct RangeMax {
private:
    int n;

public:
    vector<int> node;

    RangeMax(vector<int> v) {
        int sz = v.size();
        n = 1;
        while (n < sz) n *= 2;
        node.resize(2 * n - 1, -1);
        for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
        for (int i = n - 2; i >= 0; i--) node[i] = max(node[2 * i + 1], node[2 * i + 2]);
    }

    void update(int x, int val) {
        x += (n - 1);
        node[x] = val;
        while (x > 0) {
            x = (x - 1) / 2;
            node[x] = max(node[2 * x + 1], node[2 * x + 2]);
        }
    }

    int getmax(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return -1;
        if (a <= l && r <= b) return node[k];

        int vl = getmax(a, b, 2 * k + 1, l, (l + r) / 2);
        int vr = getmax(a, b, 2 * k + 2, (l + r) / 2, r);
        return max(vl, vr);
    }
};

/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */
const int MAX_N = 200000;
int N, K;
vector<int> table[MAX_N];
bool visited[MAX_N];

void dfs(int node) {
    visited[node] = true;

    for (int next : table[node]) {
        if (visited[next]) continue;
        dfs(next);
    }
}

signed main() {
    cin >> N >> K;
    vector<int> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];

    vector<int> diff(N-1);
    for (int i = 0; i < N - 1; i++) {
        diff[i] = A[i+1] - A[i];
    }
    RangeMin diff_seg(diff);

    int sorted_before = -1;
    for (int i = 0; i + K - 1 < N; i++) {
        if (diff_seg.getmin(i, i + K - 1) > 0) {
            if (sorted_before >= 0) {
                table[i].push_back(sorted_before);
                table[sorted_before].push_back(i);
            }

            sorted_before = i;
        }
    }

    RangeMin min_seg(A);
    RangeMax max_seg(A);

    for (int i = 0; i + K < N; i++) {
        if (A[i] <= min_seg.getmin(i, i + K + 1) && A[i+K] >= max_seg.getmax(i, i + K + 1)) {
            table[i].push_back(i+1);
            table[i+1].push_back(i);
        }
    }

    int ret = 0;
    for (int i = 0; i <= N - K; i++) {
        if (visited[i]) continue;
        ++ret;
        dfs(i);
    }

    cout << ret << endl;
    return 0;
}

Measured memory_bytes: 36196