#include "bits/stdc++.h"
using namespace std;
using ll     = long long;
using pii    = pair<int, int>;
using pll    = pair<ll, ll>;
using vi     = vector<int>;
using vl     = vector<ll>;
using vvi    = vector<vi>;
using vvl    = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}
template <class C>
void print(const C &c, std::ostream &os = std::cout) {
    std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " "));
    os << std::endl;
}

// 0-indexed bottom up Segment Tree
// UNIT is the identity element of operation func
template <typename T = int>
struct SegmentTree {
    using F = function<T(T, T)>;
    int n;
    vector<T> dat;
    F func;
    T UNIT;

    SegmentTree(int n_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) {
        n = 1;
        // full binary tree: num of leaves = n = 2^k >= n_
        while (n < n_)
            n *= 2;
        dat.assign(2 * n - 1, UNIT);
    }
    SegmentTree(vector<T> v_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) {
        n = 1;
        while (n < v_.size())
            n *= 2;
        dat.assign(2 * n - 1, UNIT);
        for (int i = 0; i < v_.size(); ++i) {
            dat[n - 1 + i] = v_[i];
        }
        for (int i = n - 2; i >= 0; --i) {
            dat[i] = func(dat[2 * i + 1], dat[2 * i + 2]);
        }
    }
    void update(int k, T a) {
        // leaves are at index n-1 to 2*n-2
        k += n - 1;
        dat[k] = a;
        while (k > 0) {
            // k -> parent node
            k = (k - 1) / 2;
            // func(child nodes)
            dat[k] = func(dat[2 * k + 1], dat[2 * k + 2]);
        }
    }
    // get result of func() in [l, r)
    T query(int l, int r) {
        l += n - 1;
        r += n - 1;
        T ret = UNIT;
        while (l < r) {
            if ((l & 1) == 0)
                ret = func(ret, dat[l]);
            if ((r & 1) == 0)
                ret = func(ret, dat[r - 1]);
            l = l / 2;
            r = (r - 1) / 2;
        }
        return ret;
    }
};

struct UnionFind {
    vector<int> rank, parent, size;

    // +1 for 1-indexed nodes
    UnionFind(int n) : rank(n + 1, 0), parent(n + 1), size(n + 1, 1) {
        iota(parent.begin(), parent.end(), 0); // parent is itself
    }
    int root(int x) {
        if (x != parent[x]) {
            parent[x] = root(parent[x]);
        }
        return parent[x];
    }
    bool isSame(int x, int y) { return root(x) == root(y); }
    bool unite(int x, int y) { return link(root(x), root(y)); }
    bool link(int x, int y) {
        if (x == y)
            return false;
        if (rank[x] > rank[y]) {
            parent[y] = x;
            size[x] += size[y];
        } else {
            parent[x] = y;
            size[y] += size[x];
            if (rank[x] == rank[y]) {
                rank[y]++;
            }
        }
        return true;
    }
    int getSize(int x) { return size[root(x)]; }
};

int main() {
    int n, k;
    cin >> n >> k;
    vi p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    auto fmin = [](int l, int r) { return min(l, r); };
    auto fmax = [](int l, int r) { return max(l, r); };
    SegmentTree<int> trmin(n, fmin, 20000000);
    SegmentTree<int> trmax(n, fmax, -1);
    for (int i = 0; i < n; ++i) {
        trmin.update(i, p[i]);
        trmax.update(i, p[i]);
    }
    UnionFind uf(n);
    int ret = 0;
    for (int i = 1; i < n - k + 1; ++i) {
        int newmin = trmin.query(i, i + k - 1);
        int oldmin = trmin.query(i - 1, i + k - 1);
        int newmax = trmax.query(i, i + k);
        int oldmax = trmax.query(i, i + k - 1);
        if (newmin > oldmin && newmax > oldmax)
            uf.unite(i, i - 1);
    }
    int l = 0;
    int r = 0;
    vi sorted(n, 1);
    for (int l = 0; l < n - k + 1; ++l) {
        if (l > r) {
            r = l;
        }
        while (r < n - 1 && p[r + 1] > p[r]) {
            r++;
        }
        sorted[l] = r - l + 1;
    }
    set<int> sortedidx;
    for (int i = 0; i < n - k + 1; ++i) {
        if (sorted[i] >= k)
            sortedidx.insert(i);
    }
    if (sortedidx.size() > 1) {
        for (auto it = next(sortedidx.begin()); it != sortedidx.end(); it = next(it)) {
            uf.unite(*it, *prev(it));
        }
    }
    set<int> used;
    for (int i = 0; i < n - k + 1; ++i) {
        if (used.count(uf.parent[i]) == 0) {
            ret++;
            used.insert(uf.parent[i]);
        }
    }
    // for (int i = 0; i < n - k + 1; ++i) {
    //     vi test = p;
    //     sort(next(test.begin(), i), next(test.begin(), i + k));
    //     print(test);
    // }
    cout << ret << "\n";
    return 0;
}

Measured memory_bytes: 18816